LinuxϵÄʵʱÈÕÖ¾¼à¿ØÓëÆÊÎö
linuxϵÄʵʱÈÕÖ¾¼à¿ØÓëÆÊÎö
ÔÚÒ»Ñùƽ³£µÄϵͳÖÎÀíºÍ¹ÊÕÏÅŲéÖУ¬ÈÕÖ¾ÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄÊý¾ÝȪԴ¡£Í¨¹ý¶ÔϵͳÈÕÖ¾µÄʵʱ¼à¿ØºÍÆÊÎö£¬ÎÒÃÇ¿ÉÒÔʵʱ·¢Ã÷Òì³£ÇéÐβ¢¾ÙÐÐÏìÓ¦µÄ´¦Àí¡£±¾ÎĽ«ÏÈÈÝLinuxÏÂÔõÑù¾ÙÐÐʵʱÈÕÖ¾¼à¿ØºÍÆÊÎö£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢ÊµÊ±ÈÕÖ¾¼à¿Ø
ÔÚLinuxÏ£¬×î³£ÓõÄÈÕ־ϵͳÊÇrsyslog¡£Í¨¹ýÉèÖÃrsyslog£¬ÎÒÃÇ¿ÉÒÔʵÏÖ½«²î±ðÓ¦ÓóÌÐòµÄÈÕÖ¾Êä³öµ½Ö¸¶¨µÄÎļþÖУ¬²¢Í¨¹ýtailÏÂÁîʵʱ¼à¿ØÕâЩÈÕÖ¾Îļþ¡£
Ê×ÏÈ£¬ÐèҪȷ±£rsyslogÒѾװÖò¢ÔËÐС£Í¨¹ýÒÔÏÂÏÂÁî¿ÉÒÔ¼ì²érsyslogµÄ״̬£º
systemctl status rsyslog
µÇ¼ºó¸´ÖÆ
±à¼rsyslogÉèÖÃÎļþ/etc/rsyslog.conf£¬½«ÐèÒª¼à¿ØµÄÈÕÖ¾Êä³öµ½Ö¸¶¨ÎļþÖС£ÀýÈ磬ÎÒÃÇÒª¼à¿Ø/var/log/messagesÎļþ£º
#½«/var/log/messagesÎļþµÄÈÕÖ¾Êä³öµ½/var/log/monitored.log :msg,contains,"kernel" /var/log/monitored.log #ÆäËûÈÕ־ĬÈÏÊä³öµ½/var/log/messages *.info;mail.none;authpriv.none;cron.none /var/log/messages
µÇ¼ºó¸´ÖÆ
ÖØÆôrsyslogЧÀÍʹÉèÖÃÉúЧ£º
service rsyslog restart
µÇ¼ºó¸´ÖÆ
ʹÓÃtailÏÂÁî¶ÔÖ¸¶¨µÄÈÕÖ¾Îļþ¾ÙÐÐʵʱ¼à¿Ø¡£ÀýÈ磬ÎÒÃÇÒª¼à¿Ø/var/log/monitored.logÎļþ£º
tail -f /var/log/monitored.log
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃǾͿÉÒÔʵʱ¼à¿ØÖ¸¶¨µÄÈÕÖ¾ÎļþÁË¡£
¶þ¡¢ÊµÊ±ÈÕÖ¾ÆÊÎö
ʵʱÈÕÖ¾¼à¿ØÖ»ÊǵÚÒ»²½£¬¸üÖ÷ÒªµÄÊǶÔÈÕÖ¾¾ÙÐÐʵʱÆÊÎö£¬ÒÔ±ãʵʱ·¢Ã÷ÎÊÌâ²¢½ÓÄÉÏìÓ¦µÄ²½·¥¡£ÔÚLinuxÏ£¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩ¹¤¾ßÀ´ÊµÏÖÈÕÖ¾ÆÊÎö¡£
awk
awkÊÇÒ»ÖÖÇ¿Ê¢µÄÎÄÌìÖ°Îö¹¤¾ß£¬ÔÚʵʱÈÕÖ¾ÆÊÎöÖо³£±»Ê¹Óá£Í¨¹ýawk£¬ÎÒÃÇ¿ÉÒÔƾָ֤¶¨µÄÌõ¼þ¶ÔÈÕÖ¾¾ÙÐйýÂ˺ʹ¦Àí¡£
ÀýÈ磬ÎÒÃÇÒª¹ýÂ˳öº¬ÓÐÌض¨Òªº¦×ÖµÄÈÕÖ¾ÐУ¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
tail -f /var/log/monitored.log | awk '/Òªº¦×Ö/'
µÇ¼ºó¸´ÖÆ
grep
grepÊÇÁíÒ»¸ö³£ÓõÄÎı¾ËÑË÷¹¤¾ß£¬¿ÉÒÔ¿ìËÙ²éÕÒ°üÀ¨Ö¸¶¨Òªº¦×ÖµÄÈÕÖ¾ÐС£
ÀýÈ磬ÎÒÃÇÒª²éÕÒº¬ÓÐÒªº¦×Ö”error”µÄÈÕÖ¾ÐУ¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
tail -f /var/log/monitored.log | grep "error"
µÇ¼ºó¸´ÖÆ
sed
sedÊÇÒ»ÖÖÁ÷ʽÎı¾±à¼Æ÷£¬¿ÉÒÔƾָ֤¶¨µÄ¹æÔò¶ÔÎı¾¾ÙÐд¦Àí¡£Í¨¹ýsed£¬ÎÒÃÇ¿ÉÒÔ¶ÔÈÕÖ¾¾ÙÐÐÌæ»»¡¢É¾³ýµÈ²Ù×÷¡£
ÀýÈ磬ÎÒÃÇÒª½«ÈÕÖ¾ÐÐÖеÄÒªº¦×Ö”warning”Ì滻Ϊ”ÖÒÑÔ”£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
tail -f /var/log/monitored.log | sed 's/warning/ÖÒÑÔ/g'
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉϹ¤¾ßµÄ×éºÏʹÓã¬ÎÒÃÇ¿ÉÒÔ¾ÙÐÐÔ½·¢ÖØ´óµÄʵʱÈÕÖ¾ÆÊÎö¡£
×ܽ᣺
ʵʱÈÕÖ¾¼à¿ØºÍÆÊÎöÔÚϵͳÖÎÀíºÍ¹ÊÕÏÅŲéÖÐÆð×ÅÖ÷ÒªµÄ×÷Óá£Í¨¹ýÉèÖÃrsyslogºÍʹÓù¤¾ßÈçawk¡¢grep¡¢sedµÈ£¬ÎÒÃÇ¿ÉÒÔʵÏÖ¶ÔLinuxϵͳÈÕÖ¾µÄʵʱ¼à¿ØºÍÆÊÎö¡£ÕâʹµÃÎÒÃÇÄܹ»ÊµÊ±·¢Ã÷ϵͳÒì³£²¢½ÓÄÉÏìÓ¦µÄ²½·¥£¬È·±£ÏµÍ³µÄÕý³£ÔËÐС£
ÒÔÉÏÊǹØÓÚlinuxϵÄʵʱÈÕÖ¾¼à¿ØÓëÆÊÎöµÄÏÈÈÝ£¬Ï£Íû¶Ô¶ÁÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇLinuxϵÄʵʱÈÕÖ¾¼à¿ØÓëÆÊÎöµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡