forked from MegaphoneJon/palante_monitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpalante_monitoring.module
More file actions
276 lines (234 loc) · 10.1 KB
/
palante_monitoring.module
File metadata and controls
276 lines (234 loc) · 10.1 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Implements hook_requirements().
*/
function palante_monitoring_requirements($phase) {
if ($phase != 'runtime') {
exit;
}
$requirements = array();
palante_monitoring_watchdog_severity($requirements);
palante_monitoring_watchdog_age($requirements);
$backupStatus = palante_monitoring_get_backup_status();
$requirements = array_merge($requirements, $backupStatus);
return $requirements;
}
/**
* Implements hook_cron().
*/
function palante_monitoring_cron() {
$requirements = array();
palante_monitoring_backups($requirements);
variable_set('palante_monitoring_backup_status', $requirements);
}
/**
* Looks up the result of the backup check cron run.
*/
function palante_monitoring_get_backup_status() {
return variable_get('palante_monitoring_backup_status');
}
/**
* Check for watchdog entries of severity "alert", "critical", or "emergency".
*/
function palante_monitoring_watchdog_severity(&$requirements) {
$sql = "SELECT wid, timestamp, severity, type, message FROM watchdog WHERE type != 'redmine' AND (severity = " . WATCHDOG_EMERGENCY . " OR severity = " . WATCHDOG_ALERT . " OR severity = " . WATCHDOG_CRITICAL . ");";
$result = db_query($sql);
// debug($result, "result");
if ($result->rowCount()) {
// We found some entries, post a warning.
$requirements['palante_monitoring_watchdog'] = array(
'title' => "Palante Monitoring - Watchdog check",
'value' => "Watchdog check found " . $result->rowCount() . " troubling entries",
'description' => "This Palante Monitoring check triggers when there are watchdog entries of \"alert\", \"emergency\", or \"critical\"",
'severity' => REQUIREMENT_ERROR,
);
}
else {
$requirements['palante_monitoring_watchdog'] = array(
'title' => "Palante Monitoring - Watchdog check",
'value' => "No alert, emergency, or critical messages found.",
'severity' => REQUIREMENT_OK,
);
}
}
/**
* Ensure the oldest watchdog entry is at least 5 days old.
*/
function palante_monitoring_watchdog_age(&$requirements) {
// get the time of the oldest watchdog log entry
$result = db_query_range('SELECT timestamp FROM watchdog ORDER BY timestamp', 0, 1)->fetchField();
// find the time since that entry
$time = "$result";
$time_difference = strtotime('now') - $time;
// put it into days
$td_days = (int) ($time_difference / 86400);
// if it's less than a day old
if ($td_days < 1) {
// post a warning.
$requirements['palante_monitoring_oldlog'] = array(
'title' => "Palante Monitoring - Oldest Log Check",
'value' => "Oldest Watchdog log is less than 1 day old!",
'description' => "This Palante Monitoring check triggers when the oldest Watchdog logs are 5 days old or less",
'severity' => REQUIREMENT_ERROR,
);
}
// if it's less than 5 days old
elseif ($td_days < 5) {
// post a warning.
$requirements['palante_monitoring_oldlog'] = array(
'title' => "Palante Monitoring - Oldest Log Check",
'value' => "Oldest Watchdog log is only " . $td_days . " days old!",
'description' => "This Palante Monitoring check triggers when the oldest Watchdog logs are less than 5 days old",
'severity' => REQUIREMENT_ERROR,
);
}
// else all is well
else {
$requirements['palante_monitoring_oldlog'] = array(
'title' => "Palante Monitoring - Oldest Log Check",
'value' => "Oldest Watchdog log is " . $td_days . " days old",
'severity' => REQUIREMENT_OK,
);
}
}
/**
* Implement a debug mode for testing run time.
*/
function palantemonitoring_watchdog($type, $message) {
// Debug mode.
$debugMode = 0;
if ($debugMode == TRUE) {
watchdog($type, $message);
}
}
function palante_monitoring_backups(&$requirements) {
// Now that we're using cron, we don't have these constants defined for us.
$REQUIREMENT_ERROR = 2;
$REQUIREMENT_OK = 0;
// Output date string to watchdog for performance testing if debugmode is enabled.
$outputdate = date("Y/m/d H:i:s") . substr((string) microtime(), 1, 6);
palantemonitoring_watchdog('palante_monitoring', "backup started at $outputdate");
// Check to see if backup migrate exists first.
if (!module_exists('backup_migrate')) {
return;
}
// Get backup destination information from backup_migrate
module_load_include('inc', 'backup_migrate', 'includes/destinations');
$destinations = backup_migrate_get_destinations();
// Filter for only those destinations which match the strings "daily" or "weekly"
$filteredDestinations = array_filter($destinations, function($obj){
if(isset($obj->location)) {
return (strpos($obj->location, "daily")) || (strpos($obj->location, "weekly"));
}
});
$result = $filteredDestinations;
// Go through each name and location found.
foreach ($result as $item) {
// Pull the information from the database and assign to variables
// Name of backup job.
$name = $item->name;
// Relative path of backup folder.
$location = $item->location;
// Convert relative path to real path.
$path = drupal_realpath($location);
// If path doesn't exist or folder is empty, exit loop and throw an error.
$gzfile = "$path/*.mysql.gz";
$gzfilecount = count(glob($gzfile));
if (($gzfilecount == 0)) {
$requirements["palante_monitoring_backup" . $name] = array(
'title' => "Palante Monitoring - " . $name . " backup",
'value' => "The backup directory " . $path . " is missing or empty!",
'severity' => $REQUIREMENT_ERROR,
);
break;
}
// Find the most recent file in that path.
$latest_mtime = 0;
$latest_filename = '';
$d = dir($path);
while (FALSE !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// Is a file, ends in gz, and the file creation time is the most recent.
if (is_file($filepath) && preg_match("/.gz$/", $filepath) && filemtime($filepath) > $latest_mtime) {
$latest_mtime = filemtime($filepath);
$latest_filename = $entry;
}
}
// Now $latest_filename contains the filename of the last changed file.
// Check first the age of the file.
// Check if backup name includes Daily and file is older than 25 hours.
if ((preg_match("/\bDaily\b/", $name)) && (time() - filemtime("{$path}/{$latest_filename}") > 25 * 3600)) {
// Get age of file in hours.
$age = (int) ((time() - filemtime("{$path}/{$latest_filename}")) / 3600);
// Output how many hours old the backup is.
$requirements["palante_monitoring_backup" . $name] = array(
'title' => "Palante Monitoring - " . $name . " backup",
'value' => "The latest " . $name . " backup, " . $latest_filename . ", is " . $age . " hours old!",
'severity' => $REQUIREMENT_ERROR,
);
}
// Check if backup name includes Weekly and file is older than 8 days.
elseif ((preg_match("/\bWeekly\b/", $name)) && (time() - filemtime("{$path}/{$latest_filename}") > 8 * 24 * 3600)) {
// Get age of file in days.
$age = (int) ((time() - filemtime("{$path}/{$latest_filename}")) / (24 * 3600));
// Output how many days old the backup is.
$requirements["palante_monitoring_backup" . $name] = array(
'title' => "Palante Monitoring - " . $name . " backup",
'value' => "The latest " . $name . " backup, " . $latest_filename . ", is " . $age . " days old!",
'severity' => $REQUIREMENT_ERROR,
);
}
// If the backup ran in the desired time period,
// then check the contents of the file.
else {
// String that we are looking for in the civi database to verify it ran correctly.
$civipattern = "CREATE TABLE `civicrm_worldregion`";
// String that we are looking for in the drupal database to verify it ran correctly.
$drupalpattern = "CREATE TABLE `watchdog`";
// Declare and empty the contents variable.
$contents = '';
// Get the full file location.
$filename = "{$path}/{$latest_filename}";
// Open the file.
$zd = gzopen($filename, "r");
// Output the file as string to contents, one line at a time.
while (!gzeof($zd)) {
$contents = gzgets($zd, 4096);
// Look through contents to see if the civi or drupal strings appear.
$civifind = strpos($contents, $civipattern);
$drupalfind = strpos($contents, $drupalpattern);
// Begin generating database check results.
if ($drupalfind !== FALSE OR $civifind !== FALSE) {
// If the desired string is found, send all is well output and break while loop.
$requirements["palante_monitoring_backup" . $name] = array(
'title' => "Palante Monitoring - " . $name . " backup",
'value' => "This backup lives at " . $path . ", and the lastest backup is " . $latest_filename,
'severity' => $REQUIREMENT_OK,
);
break;
}
// If backup doesn't contain the desired string.
elseif ((preg_match("/\bdrupal\b/", $path) && $drupalfind === FALSE) OR (preg_match("/\bcivi\b/", $path) && $civifind === FALSE) OR ($drupalfind === FALSE && $civifind === FALSE)) {
// Output that the backup did not run properly.
$requirements["palante_monitoring_backup" . $name] = array(
'title' => "Palante Monitoring - " . $name . " backup",
'value' => "The latest " . $name . " backup, " . $latest_filename . ", did not run properly!",
'severity' => $REQUIREMENT_ERROR,
);
}
// If the variables are neither false or not false,
// output that the check did not run properly.
else {
$requirements["palante_monitoring_backup" . $name] = array(
'title' => "Palante Monitoring - " . $name . " backup",
'value' => "There was an error checking the latest " . $name . " backup, " . $latest_filename . "!",
'severity' => $REQUIREMENT_ERROR,
);
}
}
}
// Output date string to watchdog for performance testing if debug mode is enabled.
$outputdate = date("Y/m/d H:i:s") . substr((string) microtime(), 1, 6);
palantemonitoring_watchdog("palante_monitoring", "backup check ended" . "$outputdate");
}
}