LinuxÏÂÈÕÖ¾ÆÊÎöÓëÈÝÆ÷¼à¿ØÒªÁìºÍÕ½ÂÔ
linuxÏÂÈÕÖ¾ÆÊÎöÓëÈÝÆ÷¼à¿ØÒªÁìºÍÕ½ÂÔ
Ëæ×ÅÔÆÅÌËãºÍÈÝÆ÷»¯ÊÖÒյĿìËÙÉú³¤£¬ÈÝÆ÷»¯°²ÅÅÒѾ³ÉΪÏÖ´úÈí¼þ¿ª·¢ºÍ°²ÅŵÄÖ÷Á÷·½·¨Ö®Ò»¡£¶øÔÚÈÝÆ÷»¯ÇéÐÎÏ£¬ÈÕÖ¾ÆÊÎöºÍÈÝÆ÷¼à¿Ø¹ØÓÚά»¤ÏµÍ³ÎȹÌÐԺ͹ÊÕÏÅŲéÊǺÜÊÇÖ÷ÒªµÄ»·½Ú¡£±¾ÎĽ«ÏÈÈÝÔÚLinuxÇéÐÎÏ£¬ÔõÑù¾ÙÐÐÈÕÖ¾ÆÊÎöºÍÈÝÆ÷¼à¿Ø£¬Í¬Ê±¸ø³öÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢ÈÕÖ¾ÆÊÎö
ÔÚLinuxÇéÐÎÏ£¬ÈÕÖ¾Îļþͨ³£´æ´¢ÔÚ/var/logĿ¼Ï£¬²î±ðЧÀÍ»òÓ¦ÓóÌÐòµÄÈÕÖ¾ÎļþλÖúÍÃûÌûáÓÐËù²î±ð¡£Õë¶Ô²î±ðµÄÈÕÖ¾Îļþ£¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩ¹¤¾ß¾ÙÐÐÆÊÎöºÍ´¦Öóͷ£¡£
grepÏÂÁî
grepÏÂÁîÊÇLinuxϳ£ÓõÄÎı¾ËÑË÷¹¤¾ß£¬ÓÃÓÚÔÚÎļþÖÐËÑË÷Ìض¨µÄ×Ö·û´®¡£Í¨¹ýgrepÏÂÁî¿ÉÒÔ¿ìËÙ¶¨Î»ÈÕÖ¾ÎļþÖеÄÒªº¦ÐÅÏ¢£¬ÀýÈç¹ýʧÐÅÏ¢¡¢Òì³£¿ÍÕ»µÈ¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁî²éÕÒ°üÀ¨Òªº¦´Ê”ERROR”µÄÈÕÖ¾ÐÅÏ¢£º
grep "ERROR" /var/log/app.log
µÇ¼ºó¸´ÖÆ
awkÏÂÁî
awkÊÇÒ»ÖÖÇ¿Ê¢µÄÎı¾´¦Öóͷ£¹¤¾ß£¬Ëü¿ÉÒÔÒÔÐÐ×÷Ϊ´¦Öóͷ£¹¤¾ß£¬¶ÔÿһÐоÙÐÐÌض¨µÄ´¦Öóͷ£¡£ÔÚÈÕÖ¾ÆÊÎöÖУ¬awkÏÂÁî¿ÉÒÔÓÃÀ´ÌáÈ¡Ö¸¶¨×Ö¶ÎÐÅÏ¢£¬²¢¾ÙÐÐÏìÓ¦µÄͳ¼ÆÆÊÎö¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁîͳ¼ÆÈÕÖ¾ÎļþÖвî±ðÈÕÖ¾¼¶±ð·ºÆðµÄ´ÎÊý£º
awk '{ count[$3]++ } END { for (level in count) print level, count[level] }' /var/log/app.log
µÇ¼ºó¸´ÖÆ
sedÏÂÁî
sedÊÇÒ»ÖÖÁ÷ʽÎı¾±à¼Æ÷£¬¿ÉÒÔƾ֤¹æÔò¶ÔÎı¾¾ÙÐд¦Öóͷ£¡£ÔÚÈÕÖ¾ÆÊÎöÖУ¬sedÏÂÁî¿ÉÒÔÓÃÀ´É¾³ýÌض¨ÐС¢Ìæ»»×Ö·û´®µÈ²Ù×÷¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁîɾ³ýÈÕÖ¾ÎļþÖаüÀ¨Òªº¦´Ê”DEBUG”µÄÐУº
sed '/DEBUG/d' /var/log/app.log
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÖ»ÊÇÈÕÖ¾ÆÊÎöÖг£ÓõÄһЩÏÂÁÏÖÕæÏàÐÎÏ¿ÉÄÜÐèҪƾ֤ÏêϸµÄÐèÇóÑ¡ÔñÊʺϵŤ¾ßºÍÒªÁì¡£
¶þ¡¢ÈÝÆ÷¼à¿Ø
ÈÝÆ÷¼à¿ØÊÇÖ¸¶ÔÔËÐÐÖеÄÈÝÆ÷¾ÙÐÐʵʱ¼à¿ØºÍÍøÂçÐÔÄÜÊý¾Ý£¬ÒÔ±ãʵʱ·¢Ã÷ÎÊÌâºÍ¾ÙÐйÊÕÏÅŲ顣ÔÚLinuxÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩ¹¤¾ßºÍÒªÁì¾ÙÐÐÈÝÆ÷¼à¿Ø¡£
cAdvisor
cAdvisorÊÇGoogle¿ªÔ´µÄÒ»¸öÈÝÆ÷¼à¿Ø¹¤¾ß£¬Ëü¿ÉÒÔ¶ÔÈÝÆ÷µÄ×ÊԴʹÓÃÇéÐΡ¢ÐÔÄÜÖ¸±êµÈ¾ÙÐмà¿Ø¡£cAdvisor¿ÉÒÔ×÷Ϊһ¸ö×ÔÁ¦µÄÈÝÆ÷ÔËÐУ¬Ò²¿ÉÒÔÓëÆäËû¼à¿Øϵͳ¼¯³ÉʹÓá£
ʹÓÃcAdvisor¾ÙÐÐÈÝÆ÷¼à¿ØºÜÊǼòÆÓ£¬Ö»ÐèÒªÔÚÔËÐÐÈÝÆ÷ʱ¼ÓÉÏÈçϲÎÊý¼´¿É£º
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest
µÇ¼ºó¸´ÖÆ
È»ºóͨ¹ý»á¼ûhttp://localhost:8080¼´¿ÉÉó²é¼à¿ØÐÅÏ¢¡£
Prometheus
PrometheusÊÇÒ»ÖÖ¿ªÔ´µÄ¼à¿ØºÍ¸æ¾¯ÏµÍ³£¬Ëü¾ßÓи߶ȿÉÀ©Õ¹ÐÔºÍÎÞаµÄÅÌÎÊÓïÑÔ¡£Í¨¹ýÔÚÈÝÆ÷Öм¯³ÉPrometheus¿Í»§¶Ë¿â£¬ÎÒÃÇ¿ÉÒÔ½«ÈÝÆ÷ÖеÄÐÔÄÜÊý¾ÝÊÕÂÞµ½PrometheusÖоÙÐмà¿ØºÍÆÊÎö¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚDockerÈÝÆ÷ÖÐʹÓÃPrometheus Python¿Í»§¶Ë¿âÀ´×Ô½ç˵ָ±êÊÕÂÞ£º
from prometheus_client import Gauge, start_http_server import time # ½¨ÉèÒ»¸öGaugeÀàÐ͵ÄÖ¸±ê metric = Gauge('custom_metric', 'This is a custom metric') if __name__ == '__main__': # Æô¶¯Ò»¸öHTTPЧÀÍÆ÷£¬ÔÚ9090¶Ë¿ÚÉÏ̻¶ָ±ê start_http_server(9090) while True: # ¸üÐÂÖ¸±êÖµ metric.set(100) time.sleep(5)
µÇ¼ºó¸´ÖÆ
È»ºóͨ¹ý»á¼ûhttp://localhost:9090/metrics¼´¿ÉÉó²é¼à¿ØÖ¸±ê¡£
ÒÔÉÏÏÈÈÝÁËÔÚLinuxÇéÐÎÏÂÈÕÖ¾ÆÊÎöºÍÈÝÆ÷¼à¿ØµÄÒªÁìºÍÕ½ÂÔ£¬Í¬Ê±¸ø³öÁËÏìÓ¦µÄ´úÂëʾÀý¡£Ï£ÍûÕâЩÄÚÈÝÄܹ»¶ÔÄã¾ÙÐÐÈÕÖ¾ÆÊÎöºÍÈÝÆ÷¼à¿ØÌṩһЩ×ÊÖú¡£ËäÈ»£¬ÏêϸµÄʵ¼ùÀú³ÌÖл¹ÐèҪƾ֤ÏÖʵÐèÇóºÍÇéÐξÙÐнøÒ»²½µÄÓÅ»¯ºÍµ÷½â¡£
ÒÔÉϾÍÊÇLinuxÏÂÈÕÖ¾ÆÊÎöÓëÈÝÆ÷¼à¿ØÒªÁìºÍÕ½ÂÔµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡