This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cron.php
151 lines (115 loc) · 5.98 KB
/
cron.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
$config = include("config.php");
include("simple_vars.php");
$load_30min = exec("cat /proc/loadavg | awk '{print $3}'");
$mem_usage_percent = floor(exec("free | grep Mem | awk '{print $3/$2 * 100.0}'"));
$swap_usage_percent = floor(exec("free | grep Swap | awk '{print $3/$2 * 100.0}'"));
//bad hdd check
exec("smartctl --scan | awk '{print $1}'", $hdd_array);
$mail_body = "========================================================<br><b>$config_hostname Report</b><br>========================================================<br>";
$mail_body .= "========================================================<br>Hard Drive Report<br>========================================================<br>";
foreach($hdd_array as $hdd){
$hdd_short_name = basename($hdd);
$hdd_status = exec("smartctl -H $hdd | grep 'overall-health' | awk '{print $6}'");
$hdd_smart = exec("smartctl -i $hdd | grep 'SMART support is' | cut -d' ' -f 8-");
$hdd_make = exec("smartctl -i $hdd | grep 'Device Model:' | awk '{print $3}'");
if($hdd_make == 'WDC'){
$hdd_make = 'Western Digital';
}else{
$hdd_make = '';
}
$hdd_vendor = exec("smartctl -i $hdd | grep 'Model Family:' | awk '{print $3,$4,$5}'");
if(empty($hdd_vendor)){
$hdd_vendor = exec("smartctl -i $hdd | grep 'Device Model:' | awk '{print $3,$4,$5}'");
}
if(empty($hdd_vendor)){
$hdd_vendor = exec("smartctl -i $hdd | grep 'Vendor:' | awk '{print $2,$3,$4}'");
}
if(empty($hdd_vendor)){
$hdd_vendor = "-";
}
$hdd_serial = exec("smartctl -i $hdd | grep 'Serial Number:' | awk '{print $3}'");
if(empty($hdd_serial)){
$hdd_serial = "-";
}
$hdd_label_size = exec("smartctl -i $hdd | grep 'User Capacity:' | cut -d '[' -f2 | cut -d ']' -f1");
$mail_body .= "$hdd_short_name - $hdd_vendor - $hdd_serial ($hdd_label_size) - $hdd_status<br>";
}
//volume usage check
exec("ls /volumes", $volume_array);
$mail_body .= "<br>========================================================<br>Volume Usage Report<br>========================================================<br>";
foreach($volume_array as $volume){
$mounted = exec("df | grep $volume");
if(!empty($mounted)){
$total_space = exec("df | grep -w /volumes/$volume | awk '{print $2}'");
$total_space_formatted = exec("df -h | grep -w /volumes/$volume | awk '{print $2}'");
$used_space = exec("df | grep -w /volumes/$volume | awk '{print $3}'");
$used_space_formatted = exec("df -h | grep -w /volumes/$volume | awk '{print $3}'");
$free_space = exec("df | grep -w /volumes/$volume | awk '{print $4}'");
$free_space_formatted = exec("df -h | grep -w /volumes/$volume | awk '{print $4}'");
$used_space_percent = exec("df | grep -w /volumes/$volume | awk '{print $5}'");
if($used_space_percent > 80){
$mail_body .= "Volume $volume is runnung out of space currently $used_space_percent of space is being used<br>";
}
}
}
$mail_body .= "<br>========================================================<br>System Resource Report<br>========================================================<br>";
if($load_30min > 5.00){
$mail_body .= "High Load: $load_30min<br>";
}
if($mem_usage_percent > 90){
$mail_body .= "High Memory usage: $mem_usage_percent<br>";
}
if($swap_usage_percent > 80){
$mail_body .= "High Swap usage: $swap_usage_percent<br>";
}
//Service Checks
$status_service_smbd = exec("systemctl status smbd | grep running");
$status_service_nmbd = exec("systemctl status nmbd | grep running");
$status_service_docker = exec("systemctl status docker | grep running");
$status_service_ssh = exec("systemctl status ssh | grep running");
$mail_body .= "<br>========================================================<br>Service Report<br>========================================================<br>";
if(empty($status_service_smbd)){
$mail_body .= "Service Stopped: Samba (smbd)<br>";
}
if(empty($status_service_nmbd)){
$mail_body .= "Service Stopped: Samba (nmbd)<br>";
}
if(empty($status_service_docker)){
$mail_body .= "Service Stopped: docker<br>";
}
if(empty($status_service_ssh)){
$mail_body .= "Service Stopped: SSH<br>";
}
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'plugins/PHPMailer/PHPMailer.php';
require 'plugins/PHPMailer/SMTP.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = $config[smtp_server]; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $config[smtp_username]; // SMTP username
$mail->Password = $config[smtp_password]; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = $config[smtp_port]; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom($config[smtp_username], "$config_hostname Bot");
$mail->addAddress($config[mail_to]); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $config_hostname . ' - Alert';
$mail->Body = $mail_body;
$mail->AltBody = $mail_body;
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>