This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_wp_update
executable file
·99 lines (86 loc) · 2.42 KB
/
check_wp_update
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
#!/bin/bash
# Version 1.9.13 (Please check against: https://github.com/fredbradley/nagios-wordpress-updates-checker/blob/master/check_wp_update )
#
# The Nagios Check that works with https://github.com/fredbradley/nagios-wordpress-updates-checker/
#
# Please read the README.md at https://github.com/fredbradley/nagios-wordpress-updates-checker/
#
# Credit Nods: Inspired by check_wp_version by @hteske. Original here: http://exchange.nagios.org/directory/Plugins/CMS-and-Blog-Software/Wordpress/check_wp_version/details
CURL=`which curl`
CURL_OPTS='--user-agent check-wp-updates-nagios-plugin --insecure -L'
BASENAME=`which basename`
PROGNAME=`$BASENAME $0`
# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
function jsonValue
{
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
function print_usage
{
echo "Usage: $PROGNAME <URL>"
}
if [ ! $1 ]; then
print_usage
exit $STATE_CRITICAL
fi
# Check that we're getting a 200 OK message for the file file on the remote host.
response=`$CURL $CURL_OPTS -I $1`
if [[ $response != *"200 OK"* ]]; then
echo 'CRITICAL - Checker Script not installed on remote host'
exit $STATE_CRITICAL
fi
# Find out if we are getting a JSON response or the legacy text (html) response
if [[ $response == *"Content-Type: application/json"* ]]; then
RESPONSE_TYPE="JSON"
else
RESPONSE_TYPE="TEXT"
fi
# Save CURL output to $result variable
result=`$CURL $CURL_OPTS -s -X GET $1`
# If there's no result, then perhaps CURL isn't installed?
if [ $? != 0 ]; then
echo 'CRITICAL - Check plugin does not work. Maybe you need to install curl.'
exit $STATE_CRITICAL
else
# Depending on the response type, then we get the content in a different way
case "$RESPONSE_TYPE" in
JSON)
status=`echo ${result[$x]} | jsonValue status 1`
text=`echo ${result[$x]} | jsonValue text 1`
;;
TEXT)
status=`echo $result | cut -d\# -f1`
text=`echo $result | cut -d\# -f2`
;;
esac
# Echo out the output
case $status in
CRITICAL|WARNING|OK)
echo "$status - $text"
;;
*)
echo "INVALID OUTPUT"
;;
esac
# Finish in a Nagios friendly way!
case "$status" in
CRITICAL)
exit $STATE_CRITICAL
;;
WARNING)
exit $STATE_WARNING
;;
OK)
exit $STATE_OK
;;
*)
exit $STATE_CRITICAL
;;
esac
fi