1. obtain IP address, CPU, memory information, and loadaverage
IP address obtained using ifconfig
Localip = $ (ifconfig eth0 | grep 'inet ADDR '| awk' {print $2} '| cut-F2-D :)
Idle CPU obtained by mpstat
Cpuidle = $ (mpstat | grep all | awk '{print $11 }')
The memory usage is obtained by free.
Freemem = $ (free | grep mem | awk '{print $4 }')
Totalmem = $ (free | grep mem | awk '{print $2 }')
Pcent =$ (free | grep mem | awk '{print $4/$2 }')
Loadaverage obtained by uptime
Loadavg1 = $ (uptime | awk '{print $10}' | cut-F1-D ,)
Loadavg5 = $ (uptime | awk '{print $11}' | cut-F1-D ,)
Loadavg15 = $ (uptime | awk '{print $12 }')
. /Etc/profilealertnginxcpu = 50 alertnginxmem = 50 alertpcent = 0.5 alertcpu = 50 alertload = 1msg = subject = 'server overload alert 'mailto () {/application/search/sendmail. PY "$ subject" "$ MSG" MSG = subject = 'server overload alert '} date = $ (date) localip = $ (/sbin/ifconfig eth0 | grep 'inet ADDR '| awk' {print $2} '| cut-F2-D :) nginxcpu = $ (PS axu | grep nginx | grep search | grep worker | awk '{print $3 }') nginxmempcent = $ (PS axu | grep nginx | grep search | grep worker | awk '{print $4 }') nginxmem = $ (PS axu | grep nginx | grep search | grep worker | awk '{print $6 }') cpuidle = $ (mpstat | grep all | awk '{print $11}') freemem = $ (free | grep mem | awk '{print $4 }') totalmem = $ (free | grep mem | awk '{print $2}') freemempcent = $ (free | grep mem | awk '{print $4/$2 }') freeswap = $ (free | grep swap | awk '{print $4}') totalswap = $ (free | grep swap | awk '{print $2 }') freeswappcent =ent (free | grep swap | awk '{print $4/$2 }') loadavg1 = $ (uptime | awk '{print $10}' | cut-F1-D ,) loadavg5 = $ (uptime | awk '{print $11}' | cut-F1-D,) loadavg15 = $ (uptime | awk '{print $12 }') echo $ dateecho $ localipecho 'idle CPU: '$ cpuidleecho 'idle memory:' $ freememecho 'total memory:' $ totalmemecho 'idle memory ratio: '$ freempcentecho' idle swap memory: '$ freeswapecho' total swap memory: '$ totalswapecho 'idle swap memory ratio:' $ freeswappcentecho $ loadavg1echo $ loadavg5echo $ loadavg15alldata = ''' $ date' | '$ localip' | nginx: '$ nginxcpu', '$ nginxmempcent' ('$ nginxmem') | cpu_idle: '$ cpuidle' | mem: '$ freemem'/'$ totalmem' = '$ freempcent' | swap: '$ freeswap'/'$ totalswap' = '$ freeswappcent' | loadavg: '$ loadavg1 ', '$ loadavg5', '$ loadavg15' 'echo $ alldata> monitor. logif [$ (echo "$ nginxcpu >=$ alertnginxcpu" | BC) = 1]; then MSG = ''$ alldata '| nginx CPU' $ nginxcpu ': higher than '$ alertnginxcpu' 'subject = ''$ subject ':' $ localip': nginx CPU '$ nginxcpu' is higher than '$ alertnginxcpu' 'echo $ MSG> monitor. log mailtofiif [$ (echo "$ nginxmempcent >=$ alertnginxmem" | BC) = 1]; then MSG = ''$ alldata' | nginx mem '$ nginxmempcent ': higher than '$ alertnginxmem' 'subject = ''$ subject ':' $ localip': nginx mem '$ nginxmempcent' is higher than '$ alertnginxmem' echo $ MSG> monitor. log mailtofiif [$ (echo "$ freeswappcent <= $ alertpcent" | BC) = 1]; then MSG = ''$ alldata '| percentage of remaining virtual memory' $ freeswappcent ': lower than '$ alertpcent ''subject ='' $ subject': '$ localip ': percentage of remaining virtual memory '$ freeswappcent' is lower than '$ alertpcent' echo $ MSG> monitor. log mailtofiif [$ (echo "$ cpuidle <= $ alertcpu" | BC) = 1]; then MSG = ''$ alldata' | CPU idle time '$ cpuidle ': lower than '$ alertcpu' subject = ''$ subject ':' $ localip': CPU idle time '$ cpuidle' is lower than '$ alertcpu' 'echo $ MSG> monitor. log mailtofiif [$ (echo "$ loadavg1 >=$ alertload" | BC) = 1]; then MSG = ''$ alldata '| load' $ loadavg1 ': higher than '$ alertload' subject = ''$ subject ':' $ localip': load' $ loadavg1 'than' $ alertload'' echo $ MSG> monitor. log mailtofiif [-N $ MSG]; Then ECHO 'System running normal' fi
2. If the idle CPU is less than the CPU alarm threshold, or the percentage of idle memory is lower than the memory alarm threshold, or the loadaverage exceeds the threshold, an email alarm is sent.
#! /usr/bin/env pythonimport smtplib import sysfrom email.mime.text import MIMEText mailto_list=[""] mail_host = "smtp.126.com" mail_user = "monitor_algo" mail_pass = "" mail_postfix="126.com" def send_mail(to_list, sub, context): me = mail_user + "<"+mail_user+"@"+mail_postfix+">" msg = MIMEText(context) msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(to_list) try: send_smtp = smtplib.SMTP() send_smtp.connect(mail_host) send_smtp.login(mail_user, mail_pass) send_smtp.sendmail(me, to_list, msg.as_string()) send_smtp.close() return True except (Exception, e): print(str(e)) return False if __name__=="__main__":# print ("start") # for a in range(1, len(sys.argv)):# print sys.argv[a] if (True == send_mail(mailto_list,sys.argv[1],sys.argv[2])): pass# print ("sucess") else: pass# print ("failed")
3. Add a crontab scheduled task
Note that the environment variables in crontan are different from those in the user environment because they do not read the environment variable parameters from the default user profile file, the simplest method is to use the source command (.)
*/1 *****./etc/profile; SH/application/search/monitor. Sh