{"id":110,"date":"2008-11-20T15:21:46","date_gmt":"2008-11-20T07:21:46","guid":{"rendered":"http:\/\/play.datalude.com\/blog\/?p=110"},"modified":"2008-12-20T13:40:29","modified_gmt":"2008-12-20T05:40:29","slug":"linux-memory-usage-summary-script","status":"publish","type":"post","link":"https:\/\/play.datalude.com\/blog\/2008\/11\/linux-memory-usage-summary-script\/","title":{"rendered":"Linux Memory Usage Summary Script"},"content":{"rendered":"<p>I was having trouble with a webserver this week, which I'd just set up for a client. When it went live, it seemed OK initially, but when we left it overnight it curled up its toes and died. It was so dead that I couldn't even ssh in to reboot it, so we had to do a remote reboot. Crunch.<\/p>\n<p>Anyway, on getting it back up, I poked around in the logs and found that it was running out of memory, which it really shouldn't do given that it was a fairly low load on the webserver, and only apache, mysql and php were running on it. I looked around the Interwebs and found an excellent resource called <a href=\"http:\/\/rimuhosting.com\/howto\/memory.jsp\" target=\"_blank\">Troubleshooting Memory Usage<\/a>, which gave me some pointers about how to rein in apache and stop it from eating up memory so quickly. Thanks!<\/p>\n<p>Anyway on the page was a script for summarising memory usage, which I thought was a good idea, so i grabbed that one and adapted it to my own purposes. I thought I'd make the results available here, so feel free to grab it and adapt it to your own needs.<!--more--><\/p>\n<pre>#!\/bin\/bash\r\n# Mem-info.sh. Version 1.5 2008-12-20\r\n# Adapted from various sources around the Internet.\r\n# Requires mutt to be installed to use the email function. \r\n\r\n#################### Functions ####################\r\nfunction print_help {\r\n\techo \"  Usage:\"\r\n\techo \"    $0 [-a full|summary] -f \/tmp\/memoryreport.txt\"\r\n\techo \"    Parameters:\"\r\n\techo \"    -a              Specify full or summary report\"\r\n\techo \"    -f              Specify alternate filename \"\r\n\techo \"    -e &lt;adddr&gt;      Send report to specified email address\"             \r\n\techo \"    -h | --help     This usage information\"\r\n\techo \"    Running the script with no parameters writes a summary report to .\/output.txt\"\r\n}\r\n\r\nfunction summary_report {\r\n\techo \"=========================== SUMMARY ============================\" &gt; $mem_outputfile\r\n\tdate &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n\techo \"=========================== uptime ==============================\" &gt;&gt; $mem_outputfile\r\n\tuptime &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n\techo \"========================== free -m ==============================\" &gt;&gt; $mem_outputfile\r\n\tfree -m &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n\techo \"========================= vmstat 1.5 ============================\" &gt;&gt; $mem_outputfile\r\n\tvmstat 1 5 &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n\techo \"================== ps top 20 Processes by CPU ===================\" &gt;&gt; $mem_outputfile\r\n\tps -eo user,%mem,%cpu,pid,cmd --sort -%cpu | head -n 20 &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n\r\n}\r\n\r\nfunction full_report {\r\n\tsummary_report\r\n\techo \"========================= FULL DETAIL ===========================\" &gt;&gt; $mem_outputfile\r\n\techo \"======================== top raw output =========================\" &gt;&gt; $mem_outputfile\r\n\ttop -b -n 1 &gt;&gt; $mem_outputfile\r\n\techo \" \" &gt;&gt; $mem_outputfile\r\n\techo \"======================= ps auxf raw output ======================\" &gt;&gt; $mem_outputfile\r\n\tps auxf --width=200 &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n\techo \"=========================== end ================================ \" &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n\techo  &gt;&gt; $mem_outputfile\r\n}\r\n\r\n#################### Process Command line ####################\r\n\r\nwhile [ \"$1\" != \"\" ]; do\r\n    case $1 in\r\n        -f )           \tshift\r\n                                mem_outputfile=$1\r\n                                ;;\r\n        -a )    \tshift\r\n\t\t\t\tmem_action=$1\r\n                                ;;\r\n        -e )            shift\r\n\t\t\t\temail_to=$1\r\n\t\t\t\t;;\r\n        -h | --help )           print_help\r\n                                exit\r\n                                ;;\r\n        * )                     print_help\r\n                                exit\r\n\t\t\t\t;;\r\n    esac\r\n    shift\r\ndone\r\n\r\n######################### Set Defaults #########################\r\nif [ -z \"${mem_action}\" ]; then \r\n    echo \"    No action Specified. Defaulting to summary report\"\r\n    mem_action=\"summary\"\r\nfi\r\nif [ -z \"${mem_outputfile}\" ]; then \r\n    echo \"    No output file specified. Writing to `pwd`\/output.txt\"\r\n    mem_outputfile=\"output.txt\"\r\nfi\r\n\r\n######################### Do Stuff #########################\r\n\r\nif [ \"$mem_action\" = \"full\" ]; then\r\n\t\tfull_report\r\n\telse\r\n\t\tsummary_report\r\nfi\r\n\r\nif [ \"$email_to\" = \"\" ]; then\r\n\t\techo \"    No email address specified. Not emailing report.\"\r\n\telse\r\n\t\tmutt -s \"Memory Usage Report\" $email_to &lt; $mem_outputfile\r\nfi<\/pre>\n<p>Or you can download the file\u00a0<a href=\"http:\/\/play.datalude.com\/blog\/wp-content\/uploads\/2008\/11\/mem-info1.sh\">mem-info1 <\/a> if the formatting above is all messed up. Make sure you go to the page I linked to above for excellent advice on interpreting the report.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was having trouble with a webserver this week, which I'd just set up for a client. When it went live, it seemed OK initially, but when we left it overnight it curled up its toes and died. It was so dead that I couldn't even ssh in to reboot it, so we had to &#8230; <a title=\"Linux Memory Usage Summary Script\" class=\"read-more\" href=\"https:\/\/play.datalude.com\/blog\/2008\/11\/linux-memory-usage-summary-script\/\" aria-label=\"Read more about Linux Memory Usage Summary Script\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,4],"tags":[95,134,94,96,85],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-it","category-linux","tag-information","tag-linux","tag-memory","tag-report-troubleshooting","tag-script"],"_links":{"self":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":0,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}