-
Notifications
You must be signed in to change notification settings - Fork 11
/
check_rss.py
executable file
·298 lines (243 loc) · 8.67 KB
/
check_rss.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
"""
check_rss - A simple Nagios plugin to check an RSS feed.
Created to monitor status of cloud services.
Requires feedparser and argparse python libraries
python-feedparser
on Debian or Redhat based systems
If you find it useful, feel free to leave me a comment/email
at http://john.wesorick.com/2011/10/nagios-plugin-checkrss.html
Copyright 2011 John Wesorick (john.wesorick.com)
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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""
import argparse
import datetime
import sys
import feedparser
def fetch_feed_last_entry(feed_url):
"""Fetch a feed from a given string"""
try:
myfeed = feedparser.parse(feed_url)
except:
output = "Could not parse URL (%s)" % feed_url
exitcritical(output, "")
if myfeed.bozo != 0:
exitcritical("Malformed feed: %s" % (myfeed.bozo_exception), "")
if myfeed.status != 200:
exitcritical("Status %s - %s" % (myfeed.status, myfeed.feed.summary), "")
# feed with 0 entries are good too
if len(myfeed.entries) == 0:
exitok("No news == good news", "")
return myfeed.entries[0]
def main(argv=None):
"""Gather user input and start the check"""
description = "A simple Nagios plugin to check an RSS feed."
epilog = """notes: If you do not specify any warning or
critical conditions, it will always return OK.
This will only check the newest feed entry.
Copyright 2011 John Wesorick (http://john.wesorick.com)"""
version = "0.3"
# Set up our arguments
parser = argparse.ArgumentParser(description=description, epilog=epilog)
parser.add_argument("--version", action="version", version=version)
parser.add_argument(
"-H",
dest="rssfeed",
help="URL of RSS feed to monitor",
action="store",
required=True,
)
parser.add_argument(
"-c",
"--criticalif",
dest="criticalif",
help="critical condition if PRESENT",
action="store",
)
parser.add_argument(
"-C",
"--criticalnot",
dest="criticalnot",
help="critical condition if MISSING",
action="store",
)
parser.add_argument(
"-w",
"--warningif",
dest="warningif",
help="warning condition if PRESENT",
action="store",
)
parser.add_argument(
"-W",
"--warningnot",
dest="warningnot",
help="warning condition if MISSING",
action="store",
)
parser.add_argument(
"-T",
"--hours",
dest="hours",
help="Hours since last post. "
"Will return critical if less than designated amount.",
action="store",
)
parser.add_argument(
"-t",
"--titleonly",
dest="titleonly",
help="Search the titles only. The default is to search "
"for strings matching in either the title or description",
action="store_true",
default=False,
)
parser.add_argument(
"-p",
"--perfdata",
dest="perfdata",
help="If used will keep very basic performance data "
"(0 if OK, 1 if WARNING, 2 if CRITICAL, 3 if UNKNOWN)",
action="store_true",
default=False,
)
parser.add_argument(
"-v",
"--verbosity",
dest="verbosity",
help="Verbosity level. 0 = Only the title and time is returned. "
"1 = Title, time and link are returned. "
"2 = Title, time, link and description are returned (Default)",
action="store",
default="2",
)
try:
args = parser.parse_args()
except:
# Something didn't work. We will return an unknown.
output = ": Invalid argument(s) {usage}".format(usage=parser.format_usage())
exitunknown(output)
perfdata = args.perfdata
# Parse our feed, getting title, description and link of newest entry.
rssfeed = args.rssfeed
if rssfeed.find("http://") != 0 and rssfeed.find("https://") != 0:
rssfeed = "http://{rssfeed}".format(rssfeed=rssfeed)
# we have everything we need, let's start
last_entry = fetch_feed_last_entry(rssfeed)
feeddate = last_entry["updated_parsed"]
title = last_entry["title"]
description = last_entry["description"]
link = last_entry["link"]
# Get the difference in time from last post
datetime_now = datetime.datetime.now()
datetime_feeddate = datetime.datetime(
*feeddate[:6]
) # http://stackoverflow.com/a/1697838/726716
timediff = datetime_now - datetime_feeddate
hourssinceposted = timediff.days * 24 + timediff.seconds / 3600
# We will form our response here based on the verbosity levels. This makes the logic below a lot easier.
if args.verbosity == "0":
output = "Posted %s hrs ago ; %s" % (hourssinceposted, title)
elif args.verbosity == "1":
output = "Posted %s hrs ago ; Title: %s; Link: %s" % (
hourssinceposted,
title,
link,
)
elif args.verbosity == "2":
output = "Posted %s hrs ago ; Title: %s ; Description: %s ; Link: %s" % (
hourssinceposted,
title,
description,
link,
)
# Check for strings that match, resulting in critical status
if args.criticalif:
criticalif = args.criticalif.lower().split(",")
for search in criticalif:
if args.titleonly:
if title.lower().find(search) >= 0:
exitcritical(output, perfdata)
else:
if (
title.lower().find(search) >= 0
or description.lower().find(search) >= 0
):
exitcritical(output, perfdata)
# Check for strings that are missing, resulting in critical status
if args.criticalnot:
criticalnot = args.criticalnot.lower().split(",")
for search in criticalnot:
if args.titleonly:
if title.lower().find(search) == -1:
exitcritical(output, perfdata)
else:
if (
title.lower().find(search) == -1
and description.lower().find(search) == -1
):
exitcritical(output, perfdata)
# Check for time difference (in hours), resulting in critical status
if args.hours:
if int(hourssinceposted) <= int(args.hours):
exitcritical(output, perfdata)
# Check for strings that match, resulting in warning status
if args.warningif:
warningif = args.warningif.lower().split(",")
for search in warningif:
if args.titleonly:
if title.lower().find(search) >= 0:
exitwarning(output, perfdata)
else:
if (
title.lower().find(search) >= 0
or description.lower().find(search) >= 0
):
exitwarning(output, perfdata)
# Check for strings that are missing, resulting in warning status
if args.warningnot:
warningnot = args.warningnot.lower().split(",")
for search in warningnot:
if args.titleonly:
if title.lower().find(search) == -1:
exitwarning(output, perfdata)
else:
if (
title.lower().find(search) == -1
and description.lower().find(search) == -1
):
exitwarning(output, perfdata)
# If we made it this far, we must be ok
exitok(output, perfdata)
def exitok(output, perfdata):
if perfdata:
print("OK - %s|'RSS'=0;1;2;0;2" % output)
else:
print("OK - %s" % output)
sys.exit(0)
def exitwarning(output, perfdata):
if perfdata:
print("WARNING - %s|'RSS'=1;1;2;0;2" % output)
else:
print("WARNING - %s" % output)
sys.exit(1)
def exitcritical(output, perfdata):
if perfdata:
print("CRITICAL - %s|'RSS'=2;1;2;0;2" % output)
else:
print("CRITICAL - %s" % output)
sys.exit(2)
def exitunknown(output):
sys.exit(3)
if __name__ == "__main__":
result = main(sys.argv)
sys.exit(result)