forked from binfalse/monitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_wp.PHP
executable file
·256 lines (224 loc) · 6.31 KB
/
check_wp.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
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
#!/usr/bin/php
<?php
#
# Copyright 2009-2017 Martin Scharm
#
# This file is part of bf-monitoring.
# <https://binfalse.de/software/nagios/>
# <https://github.com/binfalse/monitoring>
#
# bf-monitoring is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# bf-monitoring is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# bf-monitoring If not, see <http://www.gnu.org/licenses/>.
#
###################################
#
# WordPress Updates Monitoring
# written by Martin Scharm
# see http://binfalse.de
#
###################################
define ("OOK", 0);
define ("WRN", 1);
define ("ERR", 2);
define ("MMH", 3);
$instdir = null;
$domain = null;
$website = null;
$check_core = true;
$check_plugins = true;
$check_themes = true;
$verify_cert = true;
$help = false;
$version = "1.0";
$err = array ();
for ($i = 1; $i < count ($argv); $i++)
{
switch ($argv[$i])
{
case "--domain":
$domain = $argv[++$i];
break;
case "--dir":
$instdir = $argv[++$i];
break;
case "--web":
$website = $argv[++$i];
break;
case "--no-core":
$check_core = false;
break;
case "--no-plugins":
$check_plugins = false;
break;
case "--no-theme":
$check_themes = false;
break;
case "--insec-cert":
$verify_cert = false;
break;
default:
$err[] = $argv[$i]."?";
$help = true;
}
}
if (!$website && !$instdir)
$err[] = "no installation directory and no website, don't know what to check...";
if (!$check_core && !$check_plugins && !$check_themes)
$err[] = "--no-core and --no-plugins and --no-theme? you must be kidding...";
if ($instdir && !file_exists ($instdir.'/wp-load.php'))
$err[] = "your installation is way to old or your installation path isn't correct...";
if ($help || count ($err))
{
if (count ($err))
echo implode (" | ", $err)."\n";
echo "Okay, let me help you... btw. this is version $version\n";
echo "Valid arguments:\n";
echo "\t--domain DOMAIN\tcheck for DOMAIN (required for multidomain installations)\n";
echo "\t--dir DIRECTORY\twordpress installation directory can be found in DIRECTORY\n";
echo "\t--web WEBSITE\tcheck _only_ the website WEBSITE (will just check the core version for updates, based on meta name generator)\n";
echo "\t--insec-cert\tdon't verify SSL cert (in combination with --web)\n";
echo "\t--no-core\tdon't check the core\n";
echo "\t--no-plugins\tdon't check the plugins\n";
echo "\t--no-theme\tdon't check the themes\n";
echo "\t-h | --help\thelp me please\n\n";
echo "that's it for the moment...\n";
exit (MMH);
}
if ($website)
{
// just check the website...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $website);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
if (!$verify_cert)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
$data = curl_exec($ch);
curl_close($ch);
preg_match('/meta[^>]*generator[^>]*wordpress\s+([0-9.]+)/i', $data, $matches);
if (count ($matches) < 2 || !$matches[1])
{
echo "no version in web found...\n";
exit (WRN);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.wordpress.org/core/version-check/1.2/?version=" . $matches[1]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
$data = curl_exec($ch);
curl_close($ch);
$data = explode ("\n", $data);
if (version_compare ($data[3], $matches[1]) > 0)
{
echo "Your core is out of date! " . $matches[1] . " -> " . $data[3] . "\n";
exit (ERR);
}
// that's it
echo "Running " . $matches[1] . " is fine.\n";
exit (OOK);
}
// ok lets check a local installation!
if ($domain) // multihost
$_SERVER['HTTP_HOST'] = $domain;
// include wp stuff, don't need to to reinvent the wheel...
require_once($instdir.'/wp-load.php');
// let wordpress prepare it's tests
wp_version_check();
wp_update_plugins();
wp_update_themes();
// if it's pre 2.9 get_site_transient might be missing... pretty old my friend!
if (!function_exists ("get_site_transient"))
{
echo "OMG. Time to get some updates!!!";
exit (ERR);
}
$ret = OOK;
$suppl = "";
$msg = array ();
// check the core of your wordpress
if ($check_core)
{
$core = get_site_transient('update_core');
if (isset ($core->updates) && version_compare ($core->updates[0]->current, $core->version_checked) > 0)
{
$msg[] = "Core is out-of-date!";
$suppl .= "Core: " . $core->version_checked . " -> " . $core->updates[0]->current . "; ";
$ret = max ($ret, ERR);
}
else
$msg[] = "Core is up-to-date.";
}
else
$msg[] = "Skipping core checks!";
// check the plugins
if ($check_plugins)
{
$plugin_msg = array ();
$plugins = get_site_transient('update_plugins');
if (isset ($plugins->response))
{
foreach($plugins->response as $name => $update)
{
$plugin_msg[] = $update->slug . ": " . $plugins->checked[$name] ." -> " . $update->new_version;
}
}
if (count ($plugin_msg))
{
$s = "s are";
if (count ($plugin_msg) == 1)
$s = " is";
$msg[] = count ($plugin_msg) . " plugin" . $s . " out-of-date!";
$suppl .= implode ("; ", $plugin_msg) . "; ";
$ret = max ($ret, ERR);
}
else
$msg[] = "Plugins are up-to-date.";
}
else
$msg[] = "Skipping plugin checks!";
// check the themes
if ($check_themes)
{
$themes_msg = array ();
$themes = get_site_transient('update_themes');
if (isset ($themes->response))
{
foreach($themes->response as $name => $update)
{
$themes_msg[] = $name . ": " . $themes->checked[$name] ." -> " . $update["new_version"];
}
}
if (count ($themes_msg))
{
$s = "s are";
if (count ($plugin_msg) == 1)
$s = " is";
$msg[] = count ($themes_msg) . " theme" . $s . " out-of-date!";
$suppl .= implode ("; ", $themes_msg) . "; ";
$ret = max ($ret, ERR);
}
else
$msg[] = "Themes are up-to-date.";
}
else
$msg[] = "Skipping theme checks!";
// collect our info
if ($ret == OOK)
echo "Well done! ";
else
echo "Need attention! ";
echo implode (" - ", $msg) . "|" . $suppl . "\n";
exit ($ret);
?>