ÔõÑùͨ¹ýLinux¹¤¾ßʵÏÖÈÕÖ¾¼à¿ØºÍ¾¯±¨£¿
ÔõÑùͨ¹ýlinux¹¤¾ßʵÏÖÈÕÖ¾¼à¿ØºÍ¾¯±¨£¿
ÔÚÒ»Ñùƽ³£µÄЧÀÍÆ÷ÖÎÀíºÍÔËάÀú³ÌÖУ¬ÊµÊ±¼à¿ØºÍÆÊÎöÈÕÖ¾ÊǺÜÊÇÖ÷ÒªµÄ¡£LinuxϵͳÌṩÁËһЩǿʢµÄ¹¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖÈÕÖ¾¼à¿ØºÍ¾¯±¨µÄ¹¦Ð§¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃLinux¹¤¾ßÀ´¼à¿ØºÍ¾¯±¨ÈÕÖ¾£¬²¢ÌṩһЩ´úÂëʾÀý¡£
ʹÓÃtailÏÂÁîʵʱÉó²éÈÕÖ¾
tailÏÂÁî¿ÉÒÔʵʱÉó²éÈÕÖ¾ÎļþµÄ¸üÐÂÄÚÈÝ¡£Í¨¹ýʹÓÃtailÏÂÁÎÒÃÇ¿ÉÒÔÔÚÖն˴°¿ÚÖÐʵʱÏÔʾÈÕÖ¾ÎļþµÄ×îÐÂÄÚÈÝ¡£
tail -f /var/log/syslog
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁʵʱÏÔʾ/var/log/syslogÎļþµÄ×îÐÂÄÚÈÝ£¬Äã¿ÉÒÔƾ֤ÏÖÕæÏàÐÎÌæ»»Îļþ·¾¶¡£Ê¹ÓÃCtrl+C¿ÉÒÔ×èÖ¹ÈÕÖ¾µÄÉó²é¡£
ʹÓÃgrepÏÂÁî¹ýÂËÈÕÖ¾
grepÏÂÁî¿ÉÒÔÓÃÀ´¹ýÂËÈÕÖ¾Îļþ£¬Ö»ÏÔʾ֪×ãÌõ¼þµÄÐС£ÎÒÃÇ¿ÉÒÔͨ¹ýgrepÏÂÁîÀ´²éÕÒÌض¨µÄÒªº¦×Ö£¬»òÕßɨ³ýһЩҪº¦×Ö¡£
grep "error" /var/log/syslog
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁÏÔʾ/var/log/syslogÎļþÖаüÀ¨”error”Òªº¦×ÖµÄÐС£Äã¿ÉÒÔƾ֤ÏÖÕæÏàÐÎÐÞ¸ÄÒªº¦×ÖºÍÎļþ·¾¶¡£
ʹÓÃawkÏÂÁî¶ÔÈÕÖ¾¾ÙÐд¦Àí
awkÏÂÁîÊÇÒ»¸öÇ¿Ê¢µÄÎı¾´¦Àí¹¤¾ß£¬¿ÉÒÔÓÃÀ´¶ÔÈÕÖ¾Îļþ¾ÙÐд¦ÀíºÍÆÊÎö¡£ÏÂÃæÊÇÒ»¸öʾÀý£¬Í¨¹ýawkÏÂÁîͳ¼ÆÈÕÖ¾ÎļþÖÐÿ¸öIPµØµã·ºÆðµÄ´ÎÊý¡£
awk '{count[$1]++} END {for (ip in count) print ip, count[ip]}' /var/log/access.log
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁͳ¼Æ/var/log/access.logÎļþÖзºÆðµÄÿ¸öIPµØµãµÄ´ÎÊý£¬²¢½«Ð§¹ûÊä³ö¡£
ʹÓÃcron׼ʱִÐо籾
cronÊÇÒ»¸öLinuxϵͳ×Ô´øµÄ׼ʱʹÃü¹¤¾ß¡£ÎÒÃÇ¿ÉÒÔʹÓÃcronÀ´×¼Ê±Ö´ÐÐÈÕÖ¾¼à¿ØºÍ¾¯±¨¾ç±¾£¬ÒÔʵÏÖ×Ô¶¯»¯µÄÈÕÖ¾ÖÎÀí¡£
Ê×ÏÈ£¬Ê¹ÓÃcrontabÏÂÁî±à¼cronʹÃü£º
crontab -e
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÔÚ·¿ªµÄÎļþÖÐÌí¼ÓÈçÏÂÒ»ÐУ¬ÌåÏÖÿСʱִÐÐÒ»´Î¾ç±¾£º
0 * * * * /path/to/log_monitor.sh
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁÿСʱִÐÐ/path/to/log_monitor.sh¾ç±¾¡£Äã¿ÉÒÔƾ֤ÐèÒªµ÷½âÖ´ÐеÄƵÂÊ¡£
½ÓÏÂÀ´£¬±àдһ¸ölog_monitor.sh¾ç±¾£¬ÓÃÀ´¼à¿ØºÍ¾¯±¨ÈÕÖ¾¡£ÒÔÏÂÊÇÒ»¸öʾÀý£¬µ±ÈÕÖ¾ÎļþÁè¼ÝÖ¸¶¨¾Þϸʱ£¬·¢ËÍÓʼþ¸øÖÎÀíÔ±¡£
#!/bin/bash log_file="/var/log/syslog" max_size=1000000 size=$(du -b $log_file | awk '{print $1}') if [ $size -gt $max_size ]; then echo "Log file $log_file exceeds $max_size bytes" | mail -s "ÈÕÖ¾¾¯±¨" admin@example.com fi
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ¾ç±¾Ê¹ÓÃduÏÂÁî»ñÈ¡ÈÕÖ¾ÎļþµÄ¾Þϸ£¬²¢ÓëÔ¤ÉèµÄ×î´ó¾Þϸ¾ÙÐнÏÁ¿¡£ÈôÊÇÁè¼Ý×î´ó¾Þϸ£¬¾Í·¢Ë;¯±¨Óʼþ¸øÖÎÀíÔ±¡£
ͨ¹ýÒÔÉϵİ취£¬ÎÒÃÇ¿ÉÒÔʵÏÖ¶ÔÈÕÖ¾µÄʵʱ¼à¿ØºÍ¾¯±¨¹¦Ð§¡£Äã¿ÉÒÔƾ֤ÏÖʵÐèÇó£¬Ð޸ĺÍÍØÕ¹ÒÔÉϵĴúÂëʾÀý£¬ÒÔ˳Ӧ²î±ðµÄÈÕÖ¾ÖÎÀí³¡¾°¡£
ÒÔÉϾÍÊÇÔõÑùͨ¹ýLinux¹¤¾ßʵÏÖÈÕÖ¾¼à¿ØºÍ¾¯±¨£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡