Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
get running services?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
sjorge
Tux's lil' helper
Tux's lil' helper


Joined: 10 Nov 2006
Posts: 75

PostPosted: Wed Nov 15, 2006 7:57 am    Post subject: get running services? Reply with quote

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
View user's profile Send private message
mark_alec
Bodhisattva
Bodhisattva


Joined: 11 Sep 2004
Posts: 6066
Location: Melbourne, Australia

PostPosted: Wed Nov 15, 2006 8:12 am    Post subject: Reply with quote

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
View user's profile Send private message
sjorge
Tux's lil' helper
Tux's lil' helper


Joined: 10 Nov 2006
Posts: 75

PostPosted: Wed Nov 15, 2006 8:19 am    Post subject: Reply with quote

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
View user's profile Send private message
bernied
n00b
n00b


Joined: 03 Apr 2006
Posts: 50
Location: UK

PostPosted: Wed Nov 15, 2006 8:31 am    Post subject: Reply with quote

- 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
View user's profile Send private message
sjorge
Tux's lil' helper
Tux's lil' helper


Joined: 10 Nov 2006
Posts: 75

PostPosted: Wed Nov 15, 2006 9:12 am    Post subject: Reply with quote

any easy way to extract the % used from df -h?
_________________
~ sjorge
Back to top
View user's profile Send private message
drwook
Veteran
Veteran


Joined: 30 Mar 2005
Posts: 1324
Location: London

PostPosted: Wed Nov 15, 2006 9:28 am    Post subject: Reply with quote

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
View user's profile Send private message
chrbecke
Guru
Guru


Joined: 12 Jul 2004
Posts: 598
Location: Berlin - Germany

PostPosted: Wed Nov 15, 2006 9:49 am    Post subject: Reply with quote

And, if your df, like mine, doesn't support a --format option, you can use
Code:
df | awk '{print $5}'
Back to top
View user's profile Send private message
sjorge
Tux's lil' helper
Tux's lil' helper


Joined: 10 Nov 2006
Posts: 75

PostPosted: Wed Nov 15, 2006 9:58 am    Post subject: Reply with quote

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
View user's profile Send private message
JeliJami
Veteran
Veteran


Joined: 17 Jan 2006
Posts: 1086
Location: Belgium

PostPosted: Wed Nov 15, 2006 1:01 pm    Post subject: Reply with quote

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
View user's profile Send private message
bunder
Bodhisattva
Bodhisattva


Joined: 10 Apr 2004
Posts: 5947

PostPosted: Wed Nov 15, 2006 1:30 pm    Post subject: Re: get running services? Reply with quote

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. :lol:
_________________
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
View user's profile Send private message
sjorge
Tux's lil' helper
Tux's lil' helper


Joined: 10 Nov 2006
Posts: 75

PostPosted: Wed Nov 15, 2006 3:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum