ÔõÑù¾ÙÐÐLinuxϵͳµÄЧÀͼà¿ØºÍ×Ô¶¯±¨¾¯
ÔõÑù¾ÙÐÐLinuxϵͳµÄЧÀͼà¿ØºÍ×Ô¶¯±¨¾¯
СÐò£º
ÔÚÏÖ´úÐÅÏ¢ÊÖÒÕµÄÉú³¤Åä¾°Ï£¬ÆóÒµ¹ØÓÚLinuxЧÀÍÆ÷µÄÎȹÌÐԺͿɿ¿ÐÔÓÐ×ÅÔ½À´Ô½¸ßµÄÒªÇó¡£ÎªÁË°ü¹ÜЧÀÍÆ÷µÄÕý³£ÔËÐкÍÌá¸ß¹ÊÕÏ´¦ÀíЧÂÊ£¬½¨ÉèÒ»¸öÓÐÓõÄЧÀͼà¿ØºÍ×Ô¶¯±¨¾¯ÏµÍ³ÏÔµÃÓÈΪÖ÷Òª¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃPython±à³ÌÓïÑԺͿªÔ´¹¤¾ß´î½¨Ò»¸ö¼òÆÓµ«¹¦Ð§Ç¿Ê¢µÄLinuxϵͳЧÀͼà¿ØºÍ×Ô¶¯±¨¾¯ÏµÍ³£¬×ÊÖúÖÎÀíԱʵʱ·¢Ã÷ЧÀÍÆ÷¹ÊÕÏ£¬¿ìËÙÏìӦϢÕù¾öÎÊÌâ¡£
Ò»¡¢È·¶¨ÐèÒª¼à¿ØµÄЧÀÍ
Ôڴ¼à¿Øϵͳ֮ǰ£¬Ê×ÏÈҪȷ¶¨ÐèÒª¼à¿ØµÄЧÀÍ¡£³£¼ûµÄLinuxЧÀÍ°üÀ¨WebЧÀÍÆ÷£¨ÈçApache¡¢Nginx£©¡¢Êý¾Ý¿âЧÀÍÆ÷£¨ÈçMySQL¡¢PostgreSQL£©£¬ÓʼþЧÀÍÆ÷£¨ÈçSendmail¡¢Postfix£©£¬ÈÕ־ЧÀÍÆ÷£¨ÈçSyslog-ng¡¢rsyslog£©µÈ¡£Æ¾Ö¤ÏÖʵÐèÇóÑ¡ÔñÐèÒª¼à¿ØµÄЧÀÍ¡£
¼ÙÉèÎÒÃÇÐèÒª¼à¿ØWebЧÀÍÆ÷£¨Apache£©ºÍÊý¾Ý¿âЧÀÍÆ÷£¨MySQL£©¡£
¶þ¡¢Ê¹ÓÃPython¾ç±¾¾ÙÐÐЧÀͼà¿Ø
×°ÖÃÒÀÀµ¿â
Ê×ÏÈÐèҪװÖÃPythonµÄÒÀÀµ¿â£¬°üÀ¨psutil£¨ÓÃÓÚ»ñȡϵͳÐÅÏ¢£©ºÍrequests£¨ÓÃÓÚ·¢ËÍHTTPÇëÇ󣩣º
$ pip install psutil requests
µÇ¼ºó¸´ÖÆ
±àд¼à¿Ø¾ç±¾
½¨ÉèÒ»¸öÃûΪmonitor_service.pyµÄPython¾ç±¾£¬Ð´ÈëÒÔÏ´úÂ룺
import psutil import requests def check_service(process_name, url): # ¼ì²éЧÀÍÊÇ·ñÔËÐÐ for process in psutil.process_iter(['name', 'status']): if process.info['name'] == process_name: print(f"{process_name} is running") return print(f"{process_name} is not running") send_alert(process_name, url) def send_alert(process_name, url): # ·¢Ëͱ¨¾¯ÐÅÏ¢ data = { "service": process_name, "message": f"{process_name} is not running on server" } response = requests.post(url, json=data) print(response.text) if __name__ == "__main__": apache_url = "http://your_alert_server.com/alert" mysql_url = "http://your_alert_server.com/alert" check_service("apache2", apache_url) check_service("mysql", mysql_url)
µÇ¼ºó¸´ÖÆ
Ç뽫”your_alert_server.com/alert”Ì滻ΪÏÖʵµÄ±¨¾¯Ð§ÀÍÆ÷URL¡£
ÔËÐо籾
$ python monitor_service.py
µÇ¼ºó¸´ÖÆ
¾ç±¾½«¼ì²éApacheºÍMySQLЧÀÍÊÇ·ñÔËÐУ¬ÈôÊÇ·¢Ã÷ЧÀÍδÔËÐУ¬Ôò»áͨ¹ýHTTP POSTÇëÇ󽫱¨¾¯ÐÅÏ¢·¢ËÍÖÁ±¨¾¯Ð§ÀÍÆ÷¡£
Èý¡¢´î½¨×Ô¶¯±¨¾¯ÏµÍ³
´î½¨±¨¾¯Ð§ÀÍÆ÷
¿ÉÒÔʹÓÃFlaskµÈ¿ò¼Ü´î½¨Ò»¸ö¼òÆÓµÄWebЧÀÍ×÷Ϊ±¨¾¯Ð§ÀÍÆ÷£¬ÎüÊÕÀ´×Ô¼à¿Ø¾ç±¾µÄ±¨¾¯ÐÅÏ¢£¬²¢¾ÙÐÐÏìÓ¦µÄ´¦Àí£¬Èç·¢ËÍÓʼþ¡¢¶ÌÐÅ»òÍÆËÍ֪ͨµÈ¡£
±àд±¨¾¯¾ç±¾
ÔÚ±¨¾¯Ð§ÀÍÆ÷Éϱàдһ¸öÃûΪalert.pyµÄ¾ç±¾£¬ÓÃÓÚÎüÊÕ¼à¿Ø¾ç±¾·¢Ë͵ı¨¾¯ÐÅÏ¢£¬Æ¾Ö¤ÏÖʵÐèÇó¾ÙÐд¦Àí£¬ºÃ±È·¢ËÍÓʼþ±¨¾¯£º
import smtplib from email.mime.text import MIMEText def send_email(subject, content, receiver): sender = "your_email@example.com" password = "your_email_password" msg = MIMEText(content) msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver try: smtpObj = smtplib.SMTP_SSL("smtp.example.com", 465) smtpObj.login(sender, password) smtpObj.sendmail(sender, receiver, msg.as_string()) print("Email sent successfully") except Exception as e: print("Error while sending email:", str(e)) if __name__ == "__main__": # ÎüÊÕÀ´×Ô¼à¿Ø¾ç±¾µÄ±¨¾¯ÐÅÏ¢ # ƾ֤ÏÖʵÐèÇó´¦Àí±¨¾¯ÐÅÏ¢ # ·¢ËÍÓʼþ±¨¾¯Ê¾Àý data = { "service": "apache2", "message": "Apache is not running on server" } receiver = "admin@example.com" send_email("Service Alert", data["message"], receiver)
µÇ¼ºó¸´ÖÆ
Ç뽫”your_email@example.com”ºÍ”your_email_password”Ì滻ΪÏÖʵµÄ·¢¼þÓÊÏäºÍÃÜÂë¡£
ÉèÖñ¨¾¯Ð§ÀÍÆ÷
ƾ֤ÏÖÕæÏàÐν«±¨¾¯¾ç±¾alert.py°²Åŵ½±¨¾¯Ð§ÀÍÆ÷ÉÏ£¬²¢ÉèÖÃWebЧÀÍÆ÷ÒÔÎüÊÕÀ´×Ô¼à¿Ø¾ç±¾µÄ±¨¾¯ÐÅÏ¢¡£
ËÄ¡¢ÏÖʵӦÓÃÓëË¢ÐÂ
ÉÏÊöϵͳÊÇÒ»¸ö¼òÆÓµÄLinuxϵͳЧÀͼà¿ØºÍ×Ô¶¯±¨¾¯ÏµÍ³µÄ³ûÐΣ¬¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐÐˢкÍÀ©Õ¹¡£ºÃ±È¿ÉÒÔͨ¹ý׼ʱʹÃü°´ÆÚÔËÐмà¿Ø¾ç±¾£¬ÊµÏÖЧÀ͵ÄÖÜÆÚÐÔ¼à¿Ø£»¿ÉÒÔÒýÈë¸ü¶àµÄ¼à¿ØÖ¸±êºÍ¸æ¾¯·½·¨£¬ÒÔÖª×ã²î±ðµÄ¼à¿ØÐèÇó¡£
½áÂÛ£º
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇÏàʶÁËÔõÑùʹÓÃPythonºÍ¿ªÔ´¹¤¾ß´î½¨Ò»¸ö¼òÆÓµ«¹¦Ð§Ç¿Ê¢µÄLinuxϵͳЧÀͼà¿ØºÍ×Ô¶¯±¨¾¯ÏµÍ³¡£Í¨¹ý¼à¿Ø¾ç±¾ºÍ±¨¾¯Ð§ÀÍÆ÷µÄÅäºÏ£¬ÖÎÀíÔ±¿ÉÒÔʵʱ¼à²âЧÀÍÆ÷״̬£¬²¢ÊµÊ±ÏìÓ¦ºÍ´¦Àí¹ÊÕÏ£¬Ìá¸ßϵͳµÄÎȹÌÐԺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇÔõÑù¾ÙÐÐLinuxϵͳµÄЧÀͼà¿ØºÍ×Ô¶¯±¨¾¯µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡