ÔõÑùͨ¹ýLinuxÈÕÖ¾ÆÊÎöÌá¸ßϵͳÐÔÄÜ£¿
ÔõÑùͨ¹ýlinuxÈÕÖ¾ÆÊÎöÌá¸ßϵͳÐÔÄÜ£¿
ÕªÒª£ºLinuxϵͳÈÕÖ¾ÊÇϵͳÐÔÄÜÆÊÎöºÍ¹ÊÕÏÅŲéµÄÖ÷ÒªÒÀ¾Ý¡£±¾ÎĽ«ÏÈÈÝÔõÑùͨ¹ýLinuxÈÕÖ¾ÆÊÎöÀ´Ìá¸ßϵͳÐÔÄÜ£¬²¢ÌṩһЩ´úÂëʾÀýÀ´×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·¡£
СÐò£ºÔÚLinuxϵͳÖУ¬ÈÕÖ¾Îļþ°üÀ¨ÁËϵͳÔËÐÐÀú³ÌÖб¬·¢µÄÖÖÖÖÐÅÏ¢£¬°üÀ¨ÏµÍ³Àú³Ì¡¢ÍøÂçÅþÁ¬¡¢¹ýʧÐÅÏ¢µÈµÈ¡£Í¨Ì«¹ýÎöÕâЩÈÕÖ¾Îļþ£¬¿ÉÒÔ×ÊÖúÎÒÃÇ·¢Ã÷ϵͳµÄÐÔÄÜÆ¿¾±²¢½ÓÄÉÏìÓ¦µÄÓÅ»¯²½·¥¡£
Ò»¡¢Éó²éϵͳÈÕÖ¾
ÒªÆÊÎöϵͳÈÕÖ¾£¬Ê×ÏÈÐèÒªÉó²éϵͳµÄÈÕÖ¾Îļþ¡£ÔÚ´ó´ó¶¼Linux¿¯ÐаæÖУ¬ÈÕÖ¾Îļþ´æ´¢ÔÚ/var/logĿ¼Ï¡£³£¼ûµÄϵͳÈÕÖ¾Îļþ°üÀ¨£º
/var/log/messages£º°üÀ¨ÁË´ó´ó¶¼ÏµÍ³ÐÂÎź͹ýʧÐÅÏ¢¡£
/var/log/dmesg£º°üÀ¨ÁËÄÚºËÆô¶¯Ê±µÄÐÅÏ¢¡£
/var/log/syslog£º°üÀ¨ÁËϵͳÈÕÖ¾µÄËùÓÐÐÅÏ¢¡£
ʹÓÃcatÏÂÁî¿ÉÒÔÉó²éÈÕÖ¾ÎļþµÄÄÚÈÝ£¬ÀýÈ磺
cat /var/log/messages
µÇ¼ºó¸´ÖÆ
¶þ¡¢Ê¹ÓÃgrep¹ýÂËÈÕÖ¾
ϵͳÈÕÖ¾Îļþͨ³£ºÜÊÇÖØ´ó£¬Òò´ËÐèҪʹÓÃgrepÏÂÁîÀ´¹ýÂ˳öÎÒÃǹØ×¢µÄÄÚÈÝ¡£grepÏÂÁî¿ÉÒÔƾָ֤¶¨µÄÒªº¦×ÖÀ´ËÑË÷ÈÕÖ¾Îļþ£¬²¢½«Æ¥ÅäµÄÐÐÊä³ö¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔϵÄÏÂÁîÀ´²éÕÒËùÓаüÀ¨¡°error¡±µÄÐУº
grep "error" /var/log/messages
µÇ¼ºó¸´ÖÆ
Èý¡¢ÆÊÎöÈÕÖ¾ÐÅÏ¢
ͨ¹ýgrep¹ýÂ˳öÒªº¦ÐÅÏ¢ºó£¬ÎÒÃÇ¿ÉÒÔ×îÏÈÆÊÎöÈÕÖ¾ÐÅÏ¢¡£Æ¾Ö¤ÏêϸµÄÐèÇó£¬ÎÒÃÇ¿ÉÒÔ¹Ø×¢ÒÔϼ¸¸ö·½ÃæµÄÄÚÈÝ£º
CPUʹÓÃÂÊ£ºÍ¨¹ýÉó²éÈÕÖ¾ÖеÄCPUʹÓÃÂÊÐÅÏ¢£¬¿ÉÒÔÅжÏϵͳÊÇ·ñ±£´æCPUÐÔÄÜÆ¿¾±¡£
ÄÚ´æʹÓÃÇéÐΣºÍ¨¹ýÉó²éÈÕÖ¾ÖеÄÄÚ´æʹÓÃÇéÐΣ¬¿ÉÒÔÅжÏϵͳÊÇ·ñ±£´æÄÚ´æȱ·¦µÄÎÊÌâ¡£
´ÅÅÌIOÐÔÄÜ£ºÍ¨¹ýÉó²é´ÅÅÌIOÏà¹ØµÄÈÕÖ¾ÐÅÏ¢£¬¿ÉÒÔÅжÏϵͳÊÇ·ñ±£´æ´ÅÅÌIOÐÔÄÜÆ¿¾±¡£
ÍøÂçÅþÁ¬£ºÍ¨¹ýÉó²éÍøÂçÅþÁ¬Ïà¹ØµÄÈÕÖ¾ÐÅÏ¢£¬¿ÉÒÔÅжÏϵͳÊÇ·ñ±£´æÍøÂçÐÔÄÜÎÊÌâ¡£
ËÄ¡¢Ê¹ÓÃawkºÍsed¾ÙÐÐÆÊÎö
³ýÁËgrepÏÂÁÎÒÃÇ»¹¿ÉÒÔʹÓÃawkºÍsedÏÂÁî¶ÔÈÕÖ¾¾ÙÐнøÒ»²½µÄÆÊÎöºÍ´¦Öóͷ£¡£
awkÊÇÒ»¿îÇ¿Ê¢µÄÎÄÌìÖ°Îö¹¤¾ß£¬¿ÉÒÔƾָ֤¶¨µÄ¹æÔòÀ´´¦Öóͷ£Îı¾Êý¾Ý¡£ÒÔÏÂÊÇÒ»¸öʹÓÃawkÏÂÁîͳ¼ÆÈÕÖ¾ÖйýʧÐÅÏ¢ÊýÄ¿µÄʾÀý£º
awk '/error/ {count++} END {print count}' /var/log/messages
µÇ¼ºó¸´ÖÆ
sedÊÇÒ»¿îÁ÷±à¼Æ÷£¬¿ÉÒÔƾָ֤¶¨µÄ¹æÔò¶ÔÎı¾¾ÙÐÐÌæ»»ºÍ±à¼¡£ÒÔÏÂÊÇÒ»¸öʹÓÃsedÏÂÁÈÕÖ¾ÖеÄÒªº¦×ÖÌæ»»³ÉÆäËû×Ö·ûµÄʾÀý£º
sed 's/error/ERROR/g' /var/log/messages > /var/log/messages_new
µÇ¼ºó¸´ÖÆ
Îå¡¢°´ÆÚÆÊÎöÈÕÖ¾
ϵͳÈÕÖ¾ÊÇʵʱÌìÉúµÄ£¬Òò´ËÎÒÃÇÐèÒª°´ÆÚÆÊÎöºÍ¼à¿ØÈÕÖ¾Îļþ£¬ÒÔ±ãʵʱ·¢Ã÷ϵͳÐÔÄÜÎÊÌâ¡£¿ÉÒÔʹÓÃ׼ʱʹÃü£¨Èçcron£©»òÕßÉèÖÃÈÕÖ¾ÆÊÎö½ÅÔÀ´ÊµÏÖ°´ÆÚÆÊÎö¡£
Áù¡¢½áÂÛ
ͨ¹ýLinuxÈÕÖ¾ÆÊÎö£¬ÎÒÃÇ¿ÉÒÔʵʱ·¢Ã÷Ï¢Õù¾öϵͳµÄÐÔÄÜÎÊÌ⣬Ìá¸ßϵͳµÄÎȹÌÐԺͿɿ¿ÐÔ¡£±¾ÎÄÏÈÈÝÁËÔõÑùÉó²éϵͳÈÕÖ¾¡¢Ê¹ÓÃgrep¹ýÂËÈÕÖ¾¡¢ÆÊÎöÈÕÖ¾ÐÅÏ¢ÒÔ¼°Ê¹ÓÃawkºÍsed¾ÙÐнøÒ»²½ÆÊÎö¡£Ï£Íû¶ÁÕßͨ¹ý±¾ÎĵÄÄÚÈÝ£¬Äܹ»¸üºÃµØʹÓÃLinuxÈÕÖ¾ÆÊÎöÀ´Ìá¸ßϵͳÐÔÄÜ¡£
²Î¿¼´úÂëʾÀý£º
# ͳ¼ÆÈÕÖ¾ÖÐERRORÒªº¦×ÖµÄÊýÄ¿ awk '/ERROR/ {count++} END {print count}' /var/log/messages
µÇ¼ºó¸´ÖÆ
# ½«ÈÕÖ¾ÖеÄ"error"Ì滻Ϊ"ERROR" sed 's/error/ERROR/g' /var/log/messages > /var/log/messages_new
µÇ¼ºó¸´ÖÆ
# ÿ¸ô1СʱÆÊÎöÒ»Ô½ÈÕÖ¾ 0 * * * * /path/to/log_analysis.sh
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇÔõÑùͨ¹ýLinuxÈÕÖ¾ÆÊÎöÌá¸ßϵͳÐÔÄÜ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡