Home
› Blog archive
› Linux script to collect system statistics and send to your email
Posted by Marius Voila
on February 08, 2010
in London, U.K .
— This post contains 454 words
This script I use to daily send me the system statistics on my gentoo server.
It will also reformat the output to replace tabs with 5 spaces so it will display nicely on your email client.
On mail.app the fonts Monaco and Inconsolata displays nicely, but the default font does not.
requirements:
Just put the script under /etc/cron.daily
#!/bin/bash
SERVER = "mydomain.com"
EMAIL_TO = "your_email@gmail.com"
EMAIL_FROM = "stats@mydomain.com"
# logged in users and what are they running
WHO = ` w`
# processor stats
MPSTAT = ` mpstat`
# virtual memory stats
VMSTAT = ` vmstat`
# Top 20 memory hog applications
PS_MEM = ` ps -A -o pid,pcpu,pmem,start_time,state,time,comm | perl -e '($_ = join "",<>) =~ s/(\t)/ /g; print;' |sort -g -k 3 -r | head -20 `
# Top 10 CPU usage applications
PS_CPU = ` ps -A -o pid,pcpu,pmem,start_time,state,time,comm | perl -e '($_ = join "",<>) =~ s/(\t)/ /g; print;' | sort -g -k 2 -r | head -10 `
# memory usage in MB
FREE = ` free -m `
PROCINFO = ` procinfo`
# iptables status
IPTABLES = ` iptables -nL `
# established connections
NETSTAT = ` netstat -na |grep -i esta |grep -v 127.0.0.1 |sort -n -t . -k2 `
# line divider
DL = "=================================================================================="
FINAL = " ${ DL }
` date `
${ DL }
${ SERVER }
${ DL }
${ WHO }
${ DL }
${ FREE }
${ DL }
${ MPSTAT }
${ DL }
${ VMSTAT }
${ DL }
${ PROCINFO }
${ DL }
Top 10 CPU processes
${ PS_CPU }
${ DL }
Top 20 Memory processes
${ PS_MEM }
${ DL }
${ IPTABLES }
${ DL }
${ NETSTAT }
${ DL }
"
echo " ${ FINAL } " |
perl -e '($_ = join "",<>) =~ s/(\t)/ /g; print;' |
sendEmail -f " ${ EMAIL_FROM } " -u " ${ SERVER } comparator" -t ${ EMAIL_TO }
Share this:
Liked this post? Subscribe to my RSS feed .
Categories: #Tutorials
About the author:
Marius Voila is a Linux Sysadmin, a photographer, a technologist, a specialist in deployments, cloud computing, load balancing, scaling and performance tuning, as well as developing disaster-recovery best practices such as backups and restorations, firewalls, and server security audits, OpenStack deployments.
Read more →
Please enable JavaScript to view comments.
comments powered by