Board index Linux Shell Scripts

Moderator: chandranjoy

Server memory usage logger

Postby chandranjoy » Fri Oct 01, 2010 6:03 pm

Code: Select all
#!/bin/bash
log_file=/var/log/mem_usage.log
time=$(date +"%Y-%m-%d %H:%M:%S")
mem_use=$(echo `ps -eo comm,rss|awk '{arr[$1]+=$2} END {for (i in arr) {print arr[i]/1024, i}}'|grep -v '^0 '|sort -rn|head -2`)
mem_total=$(ps -eo comm,rss|awk '{arr[$1]+=$2} END {for (i in arr) {print arr[i]/1024, i}}'|grep -v '^0 '|sort -rn|awk '{s+=$1} END {print s/1024 " GB"}')
   if [ -f $log_file ]; then
      echo -e "$time\t$mem_use\t\ttotal=$mem_total" >> $log_file
   else
      touch $log_file
      echo -e "Date&Time\t\tUsed Mem|Process 1|Used Mem|Process 2\tTotal Memory used" >> $log_file
      echo -e "=================================================================================" >> $log_file
   fi
exit 1
chandranjoy
Site Admin
 
Posts: 283
Joined: Fri Oct 23, 2009 11:19 pm

Return to Shell Scripts

Who is online

Users browsing this forum: No registered users and 1 guest


cron