-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_srvr.pl
72 lines (58 loc) · 1.89 KB
/
monitor_srvr.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use strict;
use warnings;
use Net::SMTP;
my $top_cmd = "/usr/bin/top -b -n 1";
my $mem_cmd = "/usr/bin/vmstat -s";
my $disk_cmd = "/bin/df -h";
my $message;
my $cpu_threshold = 1;
my $mem_threshold = 70;
my $disk_threshold = 30;
my $top_result = `$top_cmd`;
my $mem_result = `$mem_cmd`;
my $disk_result = `$disk_cmd`;
#get cpu usage
my @top_array = split ("\n", $top_result);
my ($cpu_usage) = $top_array[2] =~ /(\d+\.\d+)%/;
my @mem_array = split ("\n", $mem_result);
my ($mem_usage) = $mem_array[1] =~ /(\d+)/;
my ($mem_total) = $mem_array[0] =~ /(\d+)/;
my ($disk_usage) = $disk_result =~ /(\d+)%/;
my $mem_percentage = sprintf ( "%.2f", (($mem_usage / $mem_total) * 100));
print "CPU raw: $top_array[2]\n";
print "CPU usage: $cpu_usage\n";
print "Memory percentage: $mem_percentage" . '%' . "\n";
print "Disk usage: $disk_usage\n";
#set threshold to send e-mail after
if ($cpu_usage > $cpu_threshold) {
$message = "CPU usage has reached $cpu_usage%\n";
#send_email($message);
#print "message1: $message\n";
}
if ($mem_percentage > $mem_threshold) {
$message = "Memory usage has reached $mem_percentage%\n";
#print "message2: $message\n";
send_email($message);
}
if ($disk_usage > $disk_threshold) {
$message = "Root disk usage has reached $disk_usage\n";
send_email($message);
}
sub send_email {
my ($message) = @_;
print "message: $message\n";
my $to_addr = '[email protected]';
my $from_addr = '[email protected]';
my $subject = "Server is overworked";
my $smtp = Net::SMTP->new('smtp.cbn.net.id') or die $!;
$smtp->mail( $from_addr);
$smtp->to( $to_addr );
$smtp->data();
$smtp->datasend("To: $to_addr\n");
$smtp->datasend("From: $from_addr\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n"); # done with header
$smtp->datasend($message);
$smtp->dataend();
$smtp->quit();
}