LinuxÏÂÈÕÖ¾ÆÊÎöÓëÒì³£¼ì²âÒªÁìºÍÕ½ÂÔ
linuxÏÂÈÕÖ¾ÆÊÎöÓëÒì³£¼ì²âÒªÁìºÍÕ½ÂÔ
СÐò£º
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬ÖÖÖÖϵͳºÍÓ¦ÓóÌÐòµÄÈÕÖ¾±¬·¢Á¿Ò²Ô½À´Ô½´ó¡£¶Ô´ó×ÚÈÕÖ¾¾ÙÐÐÓÐÓõÄÆÊÎöºÍÒì³£¼ì²â³ÉΪÁË°ü¹ÜϵͳÕý³£ÔËÐк͹ÊÕÏÕï¶ÏµÄÖ÷Òª»·½Ú¡£ÔÚLinux²Ù×÷ϵͳÖУ¬ÓÐÐí¶àÓÅÒìµÄÈÕÖ¾ÆÊÎöºÍÒì³£¼ì²â¹¤¾ß£¬±¾ÎĽ«ÏÈÈÝһЩ³£ÓõÄÒªÁìºÍÕ½ÂÔ£¬²¢ÌṩÏà¹Ø´úÂëʾÀý¡£
Ò»¡¢ÈÕÖ¾ÆÊÎöÒªÁìºÍÕ½ÂÔ
ʹÓÃgrepÏÂÁî
grepÊÇLinuxÖзǾ³£ÓõÄÏÂÁîÐй¤¾ß£¬ÓÃÓÚËÑË÷Ö¸¶¨µÄ×Ö·û´®¡£ÔÚÈÕÖ¾ÆÊÎöÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃgrepÏÂÁîÀ´²éÕÒÌض¨Òªº¦´Ê£¬ÒÔ±ãɸѡ³öÎÒÃǸÐÐËȤµÄÈÕÖ¾ÐÅÏ¢¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÕÒ³ö·ºÆð¹ÊÕϵÄÈÕÖ¾£º
grep “error” logfile
ʹÓÃawkÏÂÁî
awkÊÇÒ»ÖÖÇ¿Ê¢µÄÎı¾´¦Öóͷ£¹¤¾ß£¬ÔÚÈÕÖ¾ÆÊÎöÖпÉÒÔÓÃÀ´ÌáÈ¡ºÍ¹ýÂËÈÕÖ¾ÐÅÏ¢¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Í³¼ÆÈÕÖ¾Öвî±ðÓû§µÄ»á¼û´ÎÊý£º
awk ‘{print $1}’ logfile | sort | uniq -c
ʹÓÃsedÏÂÁî
sedÊÇÒ»ÖÖÁ÷ʽÎı¾±à¼Æ÷£¬Ò²¿ÉÒÔÓÃÓÚÈÕÖ¾ÆÊÎö¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´É¾³ýÈÕÖ¾ÖеÄʱ¼ä´Á£º
sed ‘s/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}//g’ logfile
ʹÓÃlogrotate¹¤¾ß
logrotateÊÇLinuxÖÐ×Ô´øµÄÈÕÖ¾ÂÖת¹¤¾ß£¬¿ÉÒÔÓÃÓÚÖÎÀíÈÕÖ¾ÎļþµÄ¾ÞϸºÍÊýÄ¿¡£ÎÒÃÇ¿ÉÒÔÉèÖÃlogrotateʹÆä°´ÆÚ¶ÔÈÕÖ¾Îļþ¾ÙÐÐÂÖת£¬´Ó¶ø¼á³ÖÈÕÖ¾ÎļþµÄ¿É¶ÁÐԺͿÉÖÎÀíÐÔ¡£
¶þ¡¢Òì³£¼ì²âÒªÁìºÍÕ½ÂÔ
»ùÓÚ¹æÔòµÄÒì³£¼ì²â
»ùÓÚ¹æÔòµÄÒì³£¼ì²âÊÇÒ»ÖÖ³£¼ûµÄÒªÁ죬ͨ¹ý½ç˵һϵÁйæÔòÀ´¼ì²âÈÕÖ¾ÖеÄÒì³£ÇéÐΡ£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½ç˵¹æÔò£¬µ±ÈÕÖ¾ÖзºÆðij¸öÒªº¦´Êʱ£¬´¥·¢¾¯±¨¡£¿ÉÒÔʹÓù¤¾ßÈçfail2banÀ´ÊµÏÖ»ùÓÚ¹æÔòµÄÒì³£¼ì²â¡£
»ùÓÚͳ¼ÆµÄÒì³£¼ì²â
»ùÓÚͳ¼ÆµÄÒì³£¼ì²âÊÇʹÓÃͳ¼ÆѧÔÀíÀ´¼ì²âÈÕÖ¾ÖеÄÒì³£ÇéÐΡ£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÅÌËãÈÕÖ¾ÖÐij¸öÊÂÎñµÄƽ¾ùÖµºÍ±ê×¼²î£¬µ±Ä³¸öÊÂÎñµÄÖµÁè¼Ýƽ¾ùÖµ¼ÓÉÏÈý±¶±ê×¼²îʱ£¬¾ÍÅжÏΪÒì³£¡£¿ÉÒÔʹÓù¤¾ßÈçELK£¨Elasticsearch, Logstash, Kibana£©À´¾ÙÐлùÓÚͳ¼ÆµÄÒì³£¼ì²â¡£
»ùÓÚ»úеѧϰµÄÒì³£¼ì²â
»ùÓÚ»úеѧϰµÄÒì³£¼ì²âÊÇʹÓûúеѧϰËã·¨À´ÑµÁ·Ä£×Ó£¬²¢Æ¾Ö¤Ä£×ÓÀ´ÅжÏÈÕÖ¾ÖеÄÒì³£ÇéÐΡ£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓûúеѧϰģ×ÓÀ´Õ¹ÍûÈÕÖ¾ÖпÉÄÜ·ºÆðµÄÒì³£ÊÂÎñ¡£¿ÉÒÔʹÓù¤¾ßÈçTensorFlow¡¢Scikit-learnµÈÀ´¾ÙÐлùÓÚ»úеѧϰµÄÒì³£¼ì²â¡£
´úÂëʾÀý£º
ÒÔÏÂÊÇÒ»¸öʹÓÃgrepÏÂÁîºÍshell¾ç±¾¾ÙÐÐÈÕÖ¾ÆÊÎöµÄ´úÂëʾÀý£º
#!/bin/bash logfile="access.log" keyword="error" grep $keyword $logfile | while read -r line do echo "Found error in line: $line" done
µÇ¼ºó¸´ÖÆ
ÒÔÉϾ籾»áËÑË÷ÈÕÖ¾ÎļþÖаüÀ¨¹ýʧҪº¦×ÖµÄÐУ¬²¢Êä³öÕÒµ½µÄ¹ýʧÐÅÏ¢¡£
½áÂÛ£º
ÈÕÖ¾ÆÊÎöºÍÒì³£¼ì²âÔÚLinuxϵͳÖкÜÊÇÖ÷Òª£¬Äܹ»×ÊÖúÎÒÃÇʵʱ·¢Ã÷ϵͳ¹ÊÕϺÍÒì³£ÇéÐΡ£±¾ÎÄÏÈÈÝÁËһЩ³£ÓõÄÒªÁìºÍÕ½ÂÔ£¬²¢ÌṩÁËÏà¹ØµÄ´úÂëʾÀý¡£Í¨¹ýºÏÀíʹÓÃÕâЩ¹¤¾ßºÍÒªÁ죬ÎÒÃÇ¿ÉÒÔ¸üºÃµØÆÊÎöºÍ¼ì²âÈÕÖ¾£¬°ü¹ÜϵͳµÄÕý³£ÔËÐС£
ÒÔÉϾÍÊÇLinuxÏÂÈÕÖ¾ÆÊÎöÓëÒì³£¼ì²âÒªÁìºÍÕ½ÂÔµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡