-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_wp_update.py
159 lines (136 loc) · 5.84 KB
/
check_wp_update.py
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Script to check the status of update of wordpress, based on information taken from
the API https://api.wordpress.org/core/version-check/1.7/ and site https://wordpresss.org
Creation date: 30/10/2016
Date last updated: 19/03/2017
Nagios check_wp_update plugin
*
* License: GPL
* Copyright (c) 2016 DI-FCUL
*
* Description:
*
* This file contains the check_wp_update plugin
*
* Use the nrpe program to check update information for wordpress in remote host.
*
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import sys
import urllib.request
import re
import urllib
from optparse import OptionParser
import json
import os
__author__ = "\nAuthor: Raimundo Henrique da Silva Chipongue\nE-mail: [email protected], [email protected]\nInstitution: Faculty of Science of the University of Lisbon\n"
__version__= "1.0.0"
# define exit codes
ExitOK = 0
ExitWarning = 1
ExitCritical = 2
ExitUnknown = 3
def check_connectivity():
'''
Check if the internet conection is up
'''
try:
urllib.request.urlopen('https://wordpress.org/download', timeout=1)
return True
except urllib.request.URLError:
return False
def version(opts):
if check_connectivity():
wp_installed_version_path = opts.path
def installed_wp_version():
'''
Get the installed WordPress version
'''
for line in open(wp_installed_version_path):
if "wp_version =" in line:
version_number = re.search('[-+]?\d+[\.]?\d*[\.]?\d*', line)
if version_number:
installed_version = str(version_number.group())
installed_version = installed_version.replace("-", "")
return(installed_version)
installed_version = installed_wp_version()
def current_wp_version():
'''
Get the latest stable version WordPress
'''
api_path = "https://api.wordpress.org/core/version-check/1.7/"
result = os.popen("curl -s %s "%api_path).read()
try:
latest = json.loads(result)["offers"][0]
current_version = (latest["version"])
except ValueError:
return False
if current_version == installed_version:
print('The latest stable version WordPress %s available in wordpress.org is installed'%current_version)
sys.exit(ExitOK)
else:
print('Version outdated, has installed WordPress %s, but is available in wordpress.org the version %s' %(installed_version, current_version))
sys.exit(ExitCritical)
wp_current_version = current_wp_version()
if not wp_current_version:
'''
Get the latest stable version WordPress
'''
wp_current_version_url = "https://wordpress.org/download"
values = {'s':'wordpress', 'submit':'search'}
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(wp_current_version_url, data)
resp = urllib.request.urlopen(req)
respData = resp.read()
text_version = re.findall(r'<strong>(.*?)</strong>', str(respData))
for eachP in text_version:
version_number = re.search('[-+]?\d+[\.]?\d*[\.]?\d*', eachP)
if version_number:
current_version = str(version_number.group())
current_version = current_version.replace("-", "")
if current_version == installed_version:
print('The latest stable version WordPress %s available in wordpress.org is installed' %current_version)
sys.exit(ExitOK)
else:
print('Version outdated, has installed WordPress %s, but is available in wordpress.org the version %s' %(installed_version, current_version))
sys.exit(ExitCritical)
print('Error, Connot read the current stable WordPress Version')
sys.exit(ExitUnknown)
else:
print('Error, check you internet connection')
sys.exit(ExitUnknown)
def main():
parser = OptionParser("usage: %prog [options] arg1. \nEx.: %prog -p /var/www/html/wp-includes/version.php" )
parser.add_option("-p", "--path", dest="path",
help="specify full path of version.php in wp folder installation", type="string")
parser.add_option("-V","--version", action="store_true", dest="version", help="This option show the current version number of the program and exit")
parser.add_option("-A","--author", action="store_true", dest="author", help="This option show author information and exit")
(opts, args) = parser.parse_args()
if opts.author:
print(__author__)
sys.exit()
if opts.version:
print("check_wp-update.py %s"%__version__)
sys.exit()
if not opts.path:
parser.error("This program requires at least one argument")
sys.exit(ExitUnknown)
if opts.path:
if not os.path.exists(opts.path):
parser.error("Please, this program requires to specify a valid path file.")
else:
version(opts)
else:
parser.error("Please, this program requires to specify a valid path path file.")
if __name__ == '__main__':
main()