本文實(shí)例為大家分享了python和shell監(jiān)控linux服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下
1、 shell監(jiān)控負(fù)載
監(jiān)控原理:使用uptime來獲取負(fù)載的信息,然后通過字符串截取的方式來獲取load值來獲取單個(gè)核心的負(fù)載,在將負(fù)載與閾值比較確定是否報(bào)警。
loard_monitor.sh腳本:
#!/bin/bash#使用uptime命令監(jiān)控linux系統(tǒng)負(fù)載變化#提取本服務(wù)器的IP地址信息IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`#抓取cpu的總核數(shù)cpu_num=`grep -c 'model name' /proc/cpuinfo`#抓取當(dāng)前系統(tǒng)15分鐘的平均負(fù)載值load_15=`uptime | awk '{print $NF}'`#計(jì)算當(dāng)前系統(tǒng)單個(gè)核心15分鐘的平均負(fù)載值,結(jié)果小于1.0時(shí)前面?zhèn)€位數(shù)補(bǔ)0。average_load=`echo "scale=2;a=$load_15/$cpu_num;if(length(a)==scale(a)) print 0;print a" | bc`#取上面平均負(fù)載值的個(gè)位整數(shù)average_int=`echo $average_load | cut -f 1 -d "."`#當(dāng)單個(gè)核心15分鐘的平均負(fù)載值大于等于1.0(即個(gè)位整數(shù)大于0) ,直接發(fā)郵件告警if (($average_int > 0)); then python /opt/monitor/monitor.py "服務(wù)器15分鐘的系統(tǒng)單個(gè)核心平均負(fù)載為$average_load,超過警戒值1.0,請(qǐng)立即處理?。。?fi
2、python監(jiān)控,并郵件報(bào)警,同時(shí)記錄JVM等相關(guān)參數(shù)
原理:使用crontab定時(shí)任務(wù)來執(zhí)行python腳本,在腳本中來調(diào)用shell命令或jvm命令獲取信息,最終使用python發(fā)送監(jiān)控郵件。
monitor.py
#!/usr/bin/env Python# coding=utf-8"""配合crontab來定時(shí)的讀取服務(wù)器的部分信息1、top信息2、JVM實(shí)例信息3、GC信息組裝成html發(fā)送郵件"""import smtplibimport osimport socketimport fcntlimport structimport timeimport sysfrom email.mime.text import MIMEText# 獲取本機(jī)ip和名稱def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:24])# 郵件發(fā)動(dòng)方法def send_mail(to_list, sub, content): # to_list:收件人;sub:主題;content:郵件內(nèi)容 me = mail_title + "<" + mail_user + "@" + mail_postfix + ">" # 這里的hello可以任意設(shè)置,收到信后,將按照設(shè)置顯示 msg = MIMEText(content, _subtype='html', _charset='utf-8') # 創(chuàng)建一個(gè)實(shí)例,這里設(shè)置為html格式郵件 msg['Subject'] = sub # 設(shè)置主題 msg['From'] = me msg['To'] = ";".join(to_list) try: s = smtplib.SMTP() s.connect(mail_host) # 連接smtp服務(wù)器 s.login(mail_user, mail_pass) # 登陸服務(wù)器 s.sendmail(me, to_list, msg.as_string()) # 發(fā)送郵件 s.close() return True except Exception, e: print str(e) return False# 根據(jù)shell命令返回一個(gè)list 文本def get_text_sh(bash_sh): result = os.popen(bash_sh).read() return result.split("/n")# top信息的獲取bash_top = "top -bn 1 | head -5 "top_arr_txt = get_text_sh(bash_top)# 服務(wù)器的JVM的pid 并去掉空格bash_pid = "/usr/local/java/bin/jps | grep 'Bootstrap' | awk '{print $1}'"jvm_pid = os.popen(bash_pid).read().strip()# 獲取JVM中存活得對(duì)象bash_jmap = "/usr/local/java/bin/jmap -histo:live " + jvm_pid + " | head -13 "jvm_instance_arr = get_text_sh(bash_jmap)# JVM堆信息bash_jmap_heap = "/usr/local/java/bin/jmap -heap " + jvm_pidjvm_heap_arr = get_text_sh(bash_jmap_heap)# gc統(tǒng)計(jì),采樣時(shí)間間隔為250ms,采樣數(shù)為4bash_gc = "/usr/local/java/bin/jstat -gc " + jvm_pid + " 250 4 "jvm_gc_arr = get_text_sh(bash_gc)# JVM線程快照bash_jvm_thread = "jstack -l " + jvm_pidjvm_thread_arr = get_text_sh(bash_jvm_thread)# 獲取本機(jī)名稱和IPserver_name = socket.getfqdn(socket.gethostname())# 內(nèi)網(wǎng)IPinner_ip = get_ip_address("lo")# 公網(wǎng)IPout_ip = get_ip_address("eth0")# 郵件接收者mailto_list = ["yourname@company.com"]# 設(shè)置服務(wù)器mail_host = "smtp.xxx.com"# 用戶名mail_user = "server_monitor"# 動(dòng)態(tài)客戶端口令mail_pass = "#######"# 發(fā)件箱的后綴mail_postfix = "163.com"# 標(biāo)題名稱mail_title = "ServerMonitor"# 標(biāo)題時(shí)間mail_time = time.strftime("%Y-%m-%d %X", time.localtime(time.time()))# 郵件主題mail_sub = "【監(jiān)控郵件】服務(wù)器(" + server_name + ")--IP(" + out_ip + ")--時(shí)間(" + mail_time + ")"# 目錄導(dǎo)航mail_catalog = "<ul>" / "<li><a href = '#top'>服務(wù)器top信息</a></li>" / "<li><a href = '#instance'>JVM存活實(shí)例</a></li>" / "<li><a href = '#gc'>GC情況</a></li>" / "<li><a href = '#heap'>JVM堆信息</a></li>" / "<li><a href = '#thread'>JVM線程快照及鎖</a></li>" / "</ul>"# 報(bào)警內(nèi)容mail_context = "<h2><font color='red'>" + sys.argv[1] + "</font></h2>"# 郵件正文mail_context += mail_catalog + "<h3><a name = 'top'>服務(wù)器top信息:</a></h3><hr>"# 處理top信息for line in top_arr_txt: mail_context += "<pre>" + line + "</pre>"mail_context += "<h3><a name = 'instance'>JVM存活實(shí)例10:</a></h3><hr>"# 處理jvm,并將標(biāo)簽退換掉for line in jvm_instance_arr: # 并將標(biāo)簽符號(hào)替換成html的符號(hào) mail_context += "<pre>" + line.replace("<", "<").replace(">", ">") + "</pre>"mail_context += "<h3><a name = 'gc'>GC情況 采樣時(shí)間間隔為250ms,采樣數(shù)為4:</a></h3><hr>"# 處理gc信息for line in jvm_gc_arr: # 并將標(biāo)簽符號(hào)替換成html的符號(hào) mail_context += "<pre>" + line + "</pre>"mail_context += "<h3><a name = 'heap'>JVM堆信息:</a></h3><hr>"# 處理heap信息for line in jvm_heap_arr: # 并將標(biāo)簽符號(hào)替換成html的符號(hào) mail_context += "<pre>" + line + "</pre>"mail_context += "<h3><a name = 'thread'>JVM線程快照及鎖情況:</a></h3><hr>"# 處理JVM線程快照及鎖情況for line in jvm_thread_arr: # 并將標(biāo)簽符號(hào)替換成html的符號(hào) mail_context += "<pre>" + line + "</pre>"mail_context += "<pre>線程快照過大,暫時(shí)未提供顯示,如有需要請(qǐng)聯(lián)系<a href = 'yourname'>your</pre>"# 入口if __name__ == '__main__': if send_mail(mailto_list, mail_sub, mail_context): print "發(fā)送成功" else: print "發(fā)送失敗"
新聞熱點(diǎn)
疑難解答
圖片精選