ÆÊÎöºÍÑо¿ Linux ÈÕÖ¾ÎļþÖеÄ×Ö¶Î
¡¶LinuxÈÕÖ¾ÎļþÖÐÁÐÊýµÄÆÊÎöÓëÑо¿¡·
ÔÚLinuxϵͳÖУ¬ÈÕÖ¾ÎļþÊǺÜÊÇÖ÷ÒªµÄÐÅϢȪԴ£¬¿ÉÒÔ×ÊÖúϵͳÖÎÀíÔ±¼à¿ØϵͳÔËÐÐÇéÐΡ¢ÅÅÅÌÎÊÌâÒÔ¼°¼Í¼Ҫº¦ÊÂÎñ¡£ÔÚÈÕÖ¾ÎļþÖУ¬Ã¿Ò»ÐÐͨ³£»á°üÀ¨¶à¸öÁУ¨×ֶΣ©£¬²î±ðµÄÈÕÖ¾Îļþ¿ÉÄÜ»áÓвî±ðµÄÁÐÊýºÍÃûÌ᣹ØÓÚϵͳÖÎÀíÔ±À´Ëµ£¬ÏàʶÔõÑùÓÐÓõØÆÊÎöºÍÆÊÎöÈÕÖ¾ÎļþÖеÄÁÐÊýÊǺÜÓÐÐëÒªµÄ£¬±¾ÎĽ«Ì½ÌÖÔõÑùʹÓÃLinuxÏÂÁîºÍ´úÂëʾÀýÀ´ÊµÏÖ¶ÔÈÕÖ¾ÎļþÁÐÊýµÄÆÊÎöÑо¿¡£
1. ʹÓÃawkÏÂÁîÆÊÎöÈÕÖ¾ÎļþÁÐÊý
ÔÚLinuxϵͳÖУ¬awkÊÇÒ»ÖÖÇ¿Ê¢µÄÎı¾´¦Öóͷ£¹¤¾ß£¬¿ÉÒÔÀû±ãµØ¶ÔÎı¾Îļþ¾ÙÐд¦Öóͷ£ºÍÆÊÎö¡£ÎÒÃÇ¿ÉÒÔʹÓÃawkÏÂÁîÀ´Í³¼ÆÈÕÖ¾ÎļþÖÐÿÐаüÀ¨µÄÁÐÊý£¬ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£º
awk '{print NF}' logfile
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁî»áÊä³öÈÕÖ¾ÎļþlogfileÖÐÿÐеÄÁÐÊý¡£ÆäÖÐNFÊÇawkÄÚÖõıäÁ¿£¬ÌåÏÖÿÐÐÖÐ×ֶΣ¨ÁУ©µÄÊýÄ¿¡£Í¨¹ýÔËÐÐÉÏÃæµÄÏÂÁÎÒÃÇ¿ÉÒÔ¿ìËÙ»ñµÃÈÕÖ¾ÎļþÖÐÿÐеÄÁÐÊýÇéÐΡ£
2. ʹÓÃshell¾ç±¾×Ô¶¯»¯ÆÊÎö
ΪÁ˸üºÃµØ´¦Öóͷ£´ó×ÚµÄÈÕÖ¾Îļþ²¢×Ô¶¯»¯ÆÊÎö£¬ÎÒÃÇ¿ÉÒÔ±àдһ¸ö¼òÆÓµÄshell½ÅÔÀ´ÅúÁ¿´¦Öóͷ£ÈÕÖ¾Îļþ²¢Êä³öÿÐеÄÁÐÊý¡£ÒÔÏÂÊÇÒ»¸öʾÀý¾ç±¾£º
#!/bin/bash for logfile in /var/log/*.log; do echo "Analyzing columns in $logfile" awk '{print NF}' $logfile done
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ¾ç±¾»á±éÀú/var/log/Ŀ¼ÏµÄËùÓÐÒÔ.log×îºóµÄÈÕÖ¾Îļþ£¬²¢Êä³öÿ¸öÈÕÖ¾ÎļþÖÐÿÐеÄÁÐÊý¡£Í¨¹ýÔËÐÐÕâ¸ö¾ç±¾£¬ÎÒÃÇ¿ÉÒÔÅúÁ¿ÆÊÎö¶à¸öÈÕÖ¾ÎļþµÄÁÐÊýÇéÐΡ£
3. ÆÊÎö°¸Àý
¼ÙÉèÎÒÃÇÓÐÒ»¸öÃûΪaccess.logµÄÈÕÖ¾Îļþ£¬ÄÚÈÝÈçÏ£º
2022-01-01 08:00:00 200 OK /index.html 2022-01-01 08:05:00 404 Not Found /page.html 2022-01-01 08:10:00 500 Internal Server Error /api
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔʹÓÃÇ°ÃæÌáµ½µÄawkÏÂÁî»òshell½ÅÔÀ´ÆÊÎö¸ÃÈÕÖ¾ÎļþÖÐÿÐеÄÁÐÊý¡£ÔÚÕâ¸ö°¸ÀýÖУ¬Ã¿ÐаüÀ¨ÁË4¸öÁУ¬»®·ÖÊÇʱ¼ä¡¢×´Ì¬Â롢״̬ÐÅÏ¢ºÍÇëÇóURL¡£
½áÓï
ͨ¹ý±¾ÎĵÄÆÊÎöÓëÑо¿£¬ÎÒÃÇÏàʶÁËÔõÑùʹÓÃawkÏÂÁîºÍshell½ÅÔÀ´ÆÊÎöÈÕÖ¾ÎļþÖеÄÁÐÊýÇéÐΡ£¹ØÓÚϵͳÖÎÀíÔ±À´Ëµ£¬ÕÆÎÕÕâЩ¼¼ÇÉ¿ÉÒÔ×ÊÖúËûÃǸüÓÐÓõش¦Öóͷ£ºÍÆÊÎöÈÕÖ¾Îļþ£¬ÊµÊ±·¢Ã÷ÎÊÌâ²¢¾ÙÐйÊÕÏÅŲ顣ϣÍû±¾ÎĶÔÄãÓÐËù×ÊÖú£¬½Ó´ý¼ÌÐøÉîÈëÑо¿ºÍ̽Ë÷LinuxÈÕÖ¾ÎļþµÄÆÊÎöÊÂÇé¡£
ÒÔÉϾÍÊÇÆÊÎöºÍÑо¿ Linux ÈÕÖ¾ÎļþÖеÄ×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡