LinuxÇéÐÎϵÄÈÕÖ¾ÖÎÀíºÍÆÊÎö×î¼Ñʵ¼ù
linux ÇéÐÎϵÄÈÕÖ¾ÖÎÀíºÍÆÊÎö×î¼Ñʵ¼ù
ͻ񻣼
ÈÕÖ¾ÊÇϵͳÖÐÖ÷ÒªµÄÐÅϢȪԴ£¬Äܹ»×ÊÖúÎÒÃǸú×ÙÎÊÌâ¡¢¼à¿Øϵͳ״̬ºÍÇå¾²ÐÔ¡£±¾ÎĽ«ÏÈÈÝ Linux ϵͳÖеÄÈÕÖ¾ÖÎÀíºÍÆÊÎö×î¼Ñʵ¼ù£¬°üÀ¨ÔõÑùÍøÂç¡¢´æ´¢¡¢ÆÊÎöºÍ¿ÉÊÓ»¯ÈÕÖ¾¡£±ðµÄ£¬ÎÄÕ»¹½«ÌṩһЩÊÊÓõĴúÂëʾÀýÀ´×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÓ¦ÓÃÕâЩ×î¼Ñʵ¼ù¡£
ÈÕÖ¾ÍøÂçÓë´æ´¢
ÔÚ Linux ϵͳÖУ¬ÈÕÖ¾Îļþͨ³£´æ´¢ÔÚ /var/log Ŀ¼Ï£¬ÆäÖаüÀ¨ÏµÍ³ÈÕÖ¾¡¢Ó¦ÓóÌÐòÈÕÖ¾ºÍЧÀÍÈÕÖ¾µÈ¡£ÎªÁ˸üºÃµØÖÎÀíºÍÆÊÎöÈÕÖ¾£¬ÎÒÃÇ¿ÉÒÔ½ÓÄÉÒÔÏÂʵ¼ù£º
1.1 Ñ¡ÔñºÏÊʵÄÈÕÖ¾¹¤¾ß
Linux ÌṩÁ˶àÖÖ¹¤¾ßÀ´ÍøÂçºÍ¼Í¼ϵͳÈÕÖ¾£¬ÆäÖг£¼ûµÄ¹¤¾ß°üÀ¨ syslog-ng¡¢rsyslog ºÍ journald¡£Ñ¡ÔñÊʺÏ×Ô¼ºµÄ¹¤¾ß¿ÉÒÔƾ֤ÈÕÖ¾ÐèÇóºÍϵͳÇéÐÎÀ´¾öÒé¡£
1.2 ÉèÖÃÈÕÖ¾ÂÖתսÂÔ
ÈÕÖ¾ÂÖתÊÇÒ»ÖÖ¼á³ÖÈÕÖ¾Îļþ¾ÞϸºÏÀíµÄÕ½ÂÔ£¬Ëü¿ÉÒÔ×èÖ¹ÈÕÖ¾ÎļþÎÞÏÞÔö´óµ¼ÖÂϵͳ´ÅÅÌ¿Õ¼äºÄ¾¡¡£Í¨¹ýÉèÖÃÈÕÖ¾ÂÖת¹¤¾ß£¨Èç logrotate£©£¬¿ÉÒÔ×Ô¶¯É¾³ý»òѹËõ¾ÉµÄÈÕÖ¾Îļþ£¬ÒÔ¼á³ÖϵͳµÄÕý³£ÔËÐС£
ʾÀý 1£ºlogrotate ÉèÖÃÎļþʾÀý
/var/log/syslog { rotate 7 daily missingok notifempty delaycompress compress postrotate systemctl reload rsyslog.service > /dev/null 2>&1 || true endscript }
µÇ¼ºó¸´ÖÆ
ÈÕÖ¾ÆÊÎöÓë¹ýÂË
ÈÕÖ¾ÆÊÎöÊǶԴó×ÚÈÕÖ¾¾ÙÐÐɸѡ¡¢ÌáÈ¡ºÍ»ã×Ü£¬´Ó¶ø×ÊÖúÎÒÃǸü¿ìµØÕÒµ½ÎÊÌâ¡¢ÆÊÎöϵͳÐÔÄܺͼà²âÇå¾²ÊÂÎñ¡£ÒÔÏÂÊÇһЩÈÕÖ¾ÆÊÎöÓë¹ýÂ˵Ä×î¼Ñʵ¼ù¡£
2.1 ʹÓó£¼ûµÄÈÕÖ¾¹¤¾ß
Linux ÉÏÓÐÐí¶àÇ¿Ê¢µÄÈÕÖ¾ÆÊÎö¹¤¾ß¿É¹©Ñ¡Ôñ£¬Èç grep¡¢awk¡¢sed ºÍ cut µÈ¡£ÕâЩ¹¤¾ß¿ÉÒÔÍŽáÕýÔò±í´ïʽ¡¢×Ö¶ÎÖ§½âºÍÌõ¼þ¹ýÂ˵ȹ¦Ð§£¬×ÊÖúÎÒÃǶÔÈÕÖ¾¾ÙÐпìËÙ¶¨Î»ºÍɸѡ¡£
ʾÀý 2£ºÊ¹Óà grep ¹ýÂËÈÕÖ¾
# ɸѡ°üÀ¨Ö¸¶¨Òªº¦×ÖµÄÈÕÖ¾ grep "error" /var/log/syslog # ɸѡÌØ׼ʱ¼ä¹æÄ£ÄÚµÄÈÕÖ¾ grep "2022-09-01" /var/log/syslog # ͨ¹ýÕýÔò±í´ïʽɸѡÈÕÖ¾ grep -E "(error|warning)" /var/log/syslog
µÇ¼ºó¸´ÖÆ
2.2 ʹÓÃÈÕÖ¾ÆÊÎö¹¤¾ß
³ýÁË»ù±¾µÄÏÂÁîÐй¤¾ßÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃһЩרҵµÄÈÕÖ¾ÆÊÎö¹¤¾ßÀ´´¦Àí´ó¹æÄ£µÄÈÕÖ¾Êý¾Ý¡£³£¼ûµÄÈÕÖ¾ÆÊÎö¹¤¾ß°üÀ¨ ELK Stack£¨Elasticsearch¡¢Logstash ºÍ Kibana£©¡¢Splunk ºÍ Graylog µÈ¡£
ÈÕÖ¾¿ÉÊÓ»¯Ó뾯±¨
ΪÁ˸üÖ±¹ÛµØÃ÷È·ºÍ¼à¿ØÈÕÖ¾ÐÅÏ¢£¬ÎÒÃÇ¿ÉÒÔ½«ÈÕÖ¾Êý¾Ý¿ÉÊÓ»¯ÎªÍ¼±í¡¢ÒDZíÅÌ»ò±¨±í¡£±ðµÄ£¬»ùÓÚÈÕÖ¾ÄÚÈݵľ¯±¨ÏµÍ³Ò²¿ÉÒÔ×ÊÖúÎÒÃÇʵʱ·¢Ã÷Ï¢Õù¾öϵͳÎÊÌâ¡£
3.1 ʹÓà Kibana ¿ÉÊÓ»¯ÈÕÖ¾
Kibana ÊÇÒ»¸ö¿ªÔ´µÄÈÕÖ¾ÆÊÎöºÍ¿ÉÊÓ»¯Æ½Ì¨£¬Ëü¿ÉÒÔÓë Elasticsearch ¼¯³É£¬½«ÈÕÖ¾Êý¾Ýת»¯ÎªÑŹ۵Äͼ±íºÍÒDZíÅÌ¡£
ʾÀý 3£ºÊ¹Óà Kibana ¿ÉÊÓ»¯ÈÕÖ¾
GET /logs/_search { "size": 0, "aggs": { "status_count": { "terms": { "field": "status" } } } }
µÇ¼ºó¸´ÖÆ
3.2 ÉèÖþ¯±¨ÏµÍ³
ͨ¹ýÍŽáÈÕÖ¾ÆÊÎö¹¤¾ßºÍ¼à¿Øϵͳ£¬ÎÒÃÇ¿ÉÒÔÉèÖþ¯±¨¹æÔòÀ´ÊµÊ±¼à²âϵͳ״̬ºÍÒì³£ÊÂÎñ¡£³£¼ûµÄ¾¯±¨¹¤¾ßÓÐ Zabbix¡¢Prometheus ºÍ Nagios µÈ¡£
½áÂÛ£º
ͨ¹ýºÏÀíµÄÈÕÖ¾ÖÎÀíºÍÆÊÎö£¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØÏàʶϵͳµÄÔËÐÐ״̬¡¢ÓÅ»¯ÐÔÄܺÍÌá¸ßÇå¾²ÐÔ¡£±¾ÎÄÏÈÈÝÁË linux ÇéÐÎϵÄÈÕÖ¾ÖÎÀíºÍÆÊÎö×î¼Ñʵ¼ù£¬²¢ÌṩÁËһЩÊÊÓõĴúÂëʾÀý¡£Ï£Íû¶ÁÕßÄܹ»Æ¾Ö¤×Ô¼ºµÄÐèÇóºÍÇéÐΣ¬ºÏÀíÓ¦ÓÃÕâЩʵ¼ù£¬´Ó¶ø¸üºÃµØÖÎÀíºÍÆÊÎöÈÕÖ¾Êý¾Ý¡£
ÒÔÉϾÍÊÇLinuxÇéÐÎϵÄÈÕÖ¾ÖÎÀíºÍÆÊÎö×î¼Ñʵ¼ùµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡