LinuxÉϵÄÈÕÖ¾ÆÊÎöÓëÐÔÄÜÓÅ»¯
linuxÉϵÄÈÕÖ¾ÆÊÎöÓëÐÔÄÜÓÅ»¯
ÔÚLinuxϵͳÖУ¬ÈÕÖ¾ÆÊÎöºÍÐÔÄÜÓÅ»¯ÊǺÜÊÇÖ÷ÒªµÄʹÃü¡£Í¨¹ý¶ÔϵͳÈÕÖ¾¾ÙÐÐÆÊÎö£¬ÎÒÃÇ¿ÉÒÔÏàʶϵͳµÄÔËÐÐ״̬£¬¶¨Î»ÎÊÌ⣬²¢¶Ôϵͳ¾ÙÐÐÐÔÄÜÓÅ»¯¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxÉϾÙÐÐÈÕÖ¾ÆÊÎöºÍÐÔÄÜÓÅ»¯£¬²¢ÌṩһЩ´úÂëʾÀý¡£
Ò»¡¢ÈÕÖ¾ÆÊÎö
Éó²éϵͳÈÕÖ¾
LinuxϵͳÖеÄÈÕ־ͨ³£´æ´¢ÔÚ/var/logĿ¼Ï¡£³£¼ûµÄϵͳÈÕÖ¾Îļþ°üÀ¨£º
/var/log/messages: °üÀ¨Äں˺ÍϵͳµÄÈÕÖ¾ÐÅÏ¢¡£
/var/log/secure: °üÀ¨ÏµÍ³Çå¾²Ïà¹ØµÄÈÕÖ¾ÐÅÏ¢£¬ÈçÈÏÖ¤¡¢ÊÚȨµÈ¡£
/var/log/syslog: °üÀ¨ÏµÍ³ºÍÓ¦ÓóÌÐòµÄÈÕÖ¾ÐÅÏ¢¡£
ʹÓÃÏÂÁîcat»òtail¿ÉÒÔÉó²éÈÕÖ¾ÎļþµÄÄÚÈÝ¡£ÀýÈ磬Éó²é/var/log/messagesµÄÄÚÈÝ£º
cat /var/log/messages
µÇ¼ºó¸´ÖÆ
ʹÓÃÈÕÖ¾ÆÊÎö¹¤¾ß
³ýÁËÊÖ¶¯Éó²éÈÕÖ¾Îļþ£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃһЩÈÕÖ¾ÆÊÎö¹¤¾ßÀ´×ÊÖúÆÊÎöÈÕÖ¾¡£ÆäÖУ¬×î³£ÓõŤ¾ßÊÇgrepºÍawk¡£
grep: ÓÃÓÚ²éÕÒÆ¥ÅäµÄ×Ö·û´®¡£ÀýÈ磬²éÕÒ°üÀ¨Òªº¦×Ö”error”µÄÈÕÖ¾ÐУº
grep "error" /var/log/messages
µÇ¼ºó¸´ÖÆ
awk: ÓÃÓÚ´¦Àí½á¹¹»¯µÄÎı¾Êý¾Ý¡£ÀýÈ磬ÅÌËã/var/log/messagesÖеĹýʧÈÕÖ¾ÊýÄ¿£º
awk '/error/ {count++} END {print count}' /var/log/messages
µÇ¼ºó¸´ÖÆ
ÆÊÎöÈÕÖ¾ÄÚÈÝ
ÔÚ¶ÔÈÕÖ¾¾ÙÐÐÆÊÎöʱ£¬ÎÒÃÇÐèҪעÖØһЩ³£¼ûµÄÎÊÌ⣺
¹ýʧÈÕÖ¾£º²éÕÒ²¢½â¾ö¹ýʧÈÕÖ¾£¬ÒÔ×èֹϵͳ¹ÊÕÏ¡£
ÐÔÄÜÎÊÌ⣺ÆÊÎöϵͳÈÕÖ¾ÒÔʶ±ðÐÔÄÜÎÊÌâµÄ»ù´¡Ôµ¹ÊÔÓÉ¡£
Çå¾²ÎÊÌ⣺̫ͨ¹ýÎöϵͳÈÕÖ¾À´¼ì²âºÍÔ¤·ÀÇå¾²Íþв¡£
¶þ¡¢ÐÔÄÜÓÅ»¯
Ïàʶϵͳ×ÊԴʹÓÃÇéÐÎ
ÔÚ¾ÙÐÐÐÔÄÜÓÅ»¯Ç°£¬ÎÒÃÇÐèÒªÏàʶϵͳµÄ×ÊԴʹÓÃÇéÐΡ£³£¼ûµÄ×ÊÔ´°üÀ¨CPU¡¢ÄÚ´æ¡¢´ÅÅ̺ÍÍøÂç¡£ÎÒÃÇ¿ÉÒÔʹÓÃһЩ¹¤¾ßÀ´¼à¿Øϵͳ×ÊÔ´µÄʹÓÃÇéÐΣ¬Èçtop¡¢htop¡¢freeºÍdfµÈ¡£
ÓÅ»¯ÏµÍ³ÉèÖÃ
ͨ¹ýÓÅ»¯ÏµÍ³ÉèÖã¬ÎÒÃÇ¿ÉÒÔ¸ÄÉÆϵͳµÄÐÔÄÜ¡£ÒÔÏÂÊÇһЩ³£¼ûµÄÓÅ»¯ÉèÖÃÏ
Îļþϵͳµ÷½â£ºÊ¹ÓúÏÊʵÄÎļþϵͳºÍ²ÎÊý£¬Èçext4¡¢XFSµÈ¡£
Äں˲ÎÊýµ÷½â£ºµ÷½âÄں˲ÎÊýÒÔÌá¸ßϵͳÐÔÄÜ£¬ÈçTCP/IP²ÎÊý¡¢ÎļþÐÎò·ûÏÞÖƵȡ£
ЧÀÍÓÅ»¯£ºÓÅ»¯ÏµÍ³Ð§À͵ÄÉèÖã¬ÈçApache¡¢MySQLµÈ¡£
´úÂëÓÅ»¯
ÔÚÓ¦ÓóÌÐò¿ª·¢ÖУ¬´úÂëÓÅ»¯ÊÇÌá¸ßϵͳÐÔÄܵÄÒªº¦¡£ÒÔÏÂÊÇһЩ´úÂëÓÅ»¯µÄʾÀý£º
ʹÓøßЧµÄËã·¨ºÍÊý¾Ý½á¹¹¡£
ïÔÌϵͳŲÓôÎÊý£¬Ö»¹ÜʹÓøßЧµÄAPI¡£
×èÖ¹ÄÚ´æ×ß©ºÍ×ÊÔ´ÆÌÕÅ¡£
²¢·¢±à³Ì£ºÊ¹ÓöàÏ̻߳ò¶àÀú³ÌÀ´Ìá¸ßϵͳµÄ²¢·¢ÐÔÄÜ¡£
´úÂëʾÀý£º
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> int main() { struct timeval start, end; long long sum = 0; gettimeofday(&start, NULL); for (int i = 0; i < 100000000; i++) { sum += i; } gettimeofday(&end, NULL); long long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); printf("Elapsed time: %lld microseconds ", elapsed); return 0; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÊÇÒ»¸ö¼òÆÓµÄÅÌËã1µ½1ÒÚÖ®¼äËùÓÐÕûÊýµÄºÍµÄʾÀý¡£Í¨¹ýʹÓÃʱ¼ä´Á£¨gettimeofdayº¯Êý£©£¬ÎÒÃÇ¿ÉÒÔÕÉÁ¿´úÂëµÄÖ´ÐÐʱ¼ä¡£ÈôÊÇÐèÒª¸ü׼ȷµÄÐÔÄܲâÊÔ£¬¿ÉÒÔʹÓøü¸ß¼¶µÄÐÔÄÜÆÊÎö¹¤¾ß£¬ÈçperfºÍgprof¡£
×ܽ᣺
ͨ¹ý¶ÔLinuxϵͳµÄÈÕÖ¾¾ÙÐÐÆÊÎö£¬ÎÒÃÇ¿ÉÒÔÏàʶϵͳµÄÔËÐÐ״̬£¬²¢¶¨Î»Ï¢Õù¾öÎÊÌ⡣ͬʱ£¬Í¨¹ý¾ÙÐÐÐÔÄÜÓÅ»¯£¬ÎÒÃÇ¿ÉÒÔÌá¸ßϵͳµÄÐÔÄÜ¡£Ï£Íû±¾ÎÄÌṩµÄÈÕÖ¾ÆÊÎöºÍÐÔÄÜÓÅ»¯µÄÒªÁì¶ÔÄúÓÐËù×ÊÖú¡£
²Î¿¼×ÊÁÏ£º
Linux man pages
https://www.digitalocean.com/community/tutorials/how-to-log-and-view-linux-logs-on-command-line
ÒÔÉϾÍÊÇLinuxÉϵÄÈÕÖ¾ÆÊÎöÓëÐÔÄÜÓÅ»¯µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡