View previous topic :: View next topic |
Author |
Message |
sjorge Tux's lil' helper
Joined: 10 Nov 2006 Posts: 75
|
Posted: Wed Nov 15, 2006 7:57 am Post subject: get running services? |
|
|
Hi all,
I'm trying to write my first cron job.
I'd like it to send me a weekly mail containing the following info:
- uptime got it
- mem usage / mem free
- disk space used / diskspace total
- httpd running and live got it
- mysql live got it
- sshd live got it
- mailserver live got it
I'm not sure how to get to get this info.
Help in this area would be nice.
Here is the code for anybody interested:
Code: |
#!/usr/bin/perl
# Status Report Generator
# by Jorge Schrauwen 2006
# info gathered at the gentoo forums
sub checkservice{
$srvname = $_[0];
$srvout = `/etc/init.d/$srvname status | grep -i started`;
if(length($srvout) == 0){
return "| $srvname: " . " " x (16-length($srvname)) . "stopped |\n";
}else{
return "| $srvname: " . " " x (16-length($srvname)) . "running |\n";
}
}
open(SENDMAIL, "|sendmail -t");
print SENDMAIL "From: \"Server Reporter\" <>\n";
print SENDMAIL "To: youradress\@domain.tld\n";
print SENDMAIL "Subject: System Info\n";
print SENDMAIL "Content-Type: text/plain\n\n";
print SENDMAIL "Report Date: " . `date -u` . "\n";
print SENDMAIL "+" . "-" x 18 . " System -+\n";
$memstr = `df -h -t ext3`;
$memstr = substr($memstr, -7, 4);
print SENDMAIL "| DiskSpaceUsed:" . " " x 7 . "$memstr |\n";
print SENDMAIL "| " . "-" x 25 . " |\n";
$memstr = `cat /proc/meminfo | grep -i memtotal`; chop($memstr);
print SENDMAIL "| $memstr |\n";
#$memstr = `cat /proc/meminfo | grep -i memfree`; chop($memstr);
$memstr = `free -m -k | grep -i buffers/`; chop($memstr);
$memstr = substr($memstr, -7);
print SENDMAIL "| MemFree" . " " x 8 . $memstr . " kB |\n";
print SENDMAIL "+" . "-" x 27 . "+\n\n";
print SENDMAIL "+" . "-" x 16 . " Services -+\n";
print SENDMAIL checkservice('apache2');
print SENDMAIL checkservice('mysql');
print SENDMAIL checkservice('named');
print SENDMAIL checkservice('sshd');
print SENDMAIL "+" . "-" x 27 . "+\n\n";
print SENDMAIL "+" . "-" x 19 . " Hosts -+\n";
[b]opendir(HDIR, "/home/");[/b]
while (my $file = readdir(HDIR)){
if(($file ne '.') and ($file ne '..')){
if(-d "/server/hosts/$file"){
print SENDMAIL "| $file" . " " x (25-length($file)) . " |\n";
}
}
}
print SENDMAIL "+" . "-" x 27 . "+\n";
print SENDMAIL ".\n";
close(SENDMAIL);
|
_________________ ~ sjorge
Last edited by sjorge on Wed Nov 15, 2006 3:41 pm; edited 3 times in total |
|
Back to top |
|
|
mark_alec Bodhisattva
Joined: 11 Sep 2004 Posts: 6066 Location: Melbourne, Australia
|
Posted: Wed Nov 15, 2006 8:12 am Post subject: |
|
|
Don't know how to get it to send you the email, but to find out the status of a service run `/etc/init.d/<service> status`. _________________ www.gentoo.org.au || #gentoo-au |
|
Back to top |
|
|
sjorge Tux's lil' helper
Joined: 10 Nov 2006 Posts: 75
|
Posted: Wed Nov 15, 2006 8:19 am Post subject: |
|
|
mark_alec wrote: | Don't know how to get it to send you the email, but to find out the status of a service run `/etc/init.d/<service> status`. |
doh so simple! thanks this puts me another step closer!
I was thinking of going the netstat way and parse the output but his is 100x simpler! _________________ ~ sjorge |
|
Back to top |
|
|
bernied n00b
Joined: 03 Apr 2006 Posts: 50 Location: UK
|
Posted: Wed Nov 15, 2006 8:31 am Post subject: |
|
|
- mem usage / mem free
cat /proc/meminfo
- disk space used / diskspace total
df -h
this is not strictly diskspace, rather filesystems, but might be what you want |
|
Back to top |
|
|
sjorge Tux's lil' helper
Joined: 10 Nov 2006 Posts: 75
|
Posted: Wed Nov 15, 2006 9:12 am Post subject: |
|
|
any easy way to extract the % used from df -h? _________________ ~ sjorge |
|
Back to top |
|
|
drwook Veteran
Joined: 30 Mar 2005 Posts: 1324 Location: London
|
Posted: Wed Nov 15, 2006 9:28 am Post subject: |
|
|
sjorge wrote: | any easy way to extract the % used from df -h? |
Code: | df --format=capacity | perhaps? The man pages are your friends...
[edit] for mem usage, is 'free -m' better for you?[/edit] |
|
Back to top |
|
|
chrbecke Guru
Joined: 12 Jul 2004 Posts: 598 Location: Berlin - Germany
|
Posted: Wed Nov 15, 2006 9:49 am Post subject: |
|
|
And, if your df, like mine, doesn't support a --format option, you can use Code: | df | awk '{print $5}' |
|
|
Back to top |
|
|
sjorge Tux's lil' helper
Joined: 10 Nov 2006 Posts: 75
|
Posted: Wed Nov 15, 2006 9:58 am Post subject: |
|
|
chrbecke wrote: | And, if your df, like mine, doesn't support a --format option, you can use Code: | df | awk '{print $5}' |
|
Indeed it didn't, this works fine thanks!
Now all i need a nice uptime than the uptime command displays i get hh:mm:ss but nothing with days and such _________________ ~ sjorge |
|
Back to top |
|
|
JeliJami Veteran
Joined: 17 Jan 2006 Posts: 1086 Location: Belgium
|
Posted: Wed Nov 15, 2006 1:01 pm Post subject: |
|
|
sjorge wrote: | Now all i need a nice uptime than the uptime command displays i get hh:mm:ss but nothing with days and such |
Mine does:
Code: | # uptime
14:00:15 up 26 days, 20:47, 0 users, load average: 0.00, 0.00, 0.00 |
_________________ Unanswered Post Initiative | Search | FAQ
Former username: davjel |
|
Back to top |
|
|
bunder Bodhisattva
Joined: 10 Apr 2004 Posts: 5947
|
Posted: Wed Nov 15, 2006 1:30 pm Post subject: Re: get running services? |
|
|
sjorge wrote: | Hi all,
I'm trying to write my first cron job.
I'd like it to send me a weekly mail containing the following info:
- mem usage / mem free
- disk space used / diskspace total
I'm not sure how to get to get this info.
Help in this area would be nice. |
free -m
df -h (AND df -ih if you are using an inode based filesystem)
edit: i should really start reading the responses before i post. _________________
Neddyseagoon wrote: | The problem with leaving is that you can only do it once and it reduces your influence. |
banned from #gentoo since sept 2017 |
|
Back to top |
|
|
sjorge Tux's lil' helper
Joined: 10 Nov 2006 Posts: 75
|
Posted: Wed Nov 15, 2006 3:38 pm Post subject: |
|
|
Thanks for all the help I'm done now. I'll update my main post with the code for other people interested in this.
davjel wrote: | sjorge wrote: | Now all i need a nice uptime than the uptime command displays i get hh:mm:ss but nothing with days and such |
Mine does:
Code: | # uptime
14:00:15 up 26 days, 20:47, 0 users, load average: 0.00, 0.00, 0.00 |
|
Mine reports this aswel, I was looking for something along the lines of
0 days, 2 hours, 53 minutes, I just left it out since it seems to vary in how it displays it depending on the uptime -_- _________________ ~ sjorge |
|
Back to top |
|
|
|