-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc-prtg-generator.py
401 lines (317 loc) · 13.4 KB
/
pc-prtg-generator.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
import sys
import argparse
import re
import ssl
import xml.etree.ElementTree as ET
from enum import Enum
from csv import reader
from urllib.request import Request, urlopen, URLError, build_opener, install_opener, HTTPSHandler
from urllib.parse import urlparse
from itertools import islice
from codecs import iterdecode
from uuid import uuid4
# Constants
PRINTER_SERVER_COLUMN = 0
PRINTER_NAME_COLUMN = 1
PRINTER_LOCATION_COLUMN = 2
PRINTER_URL_COLUMN = 3
DEVICE_NAME_COLUMN = 0
DEVICE_LOCATION_COLUMN = 1
DEVICE_URL_COLUMN = 2
class CheckType(Enum):
PING = 1
DOTNET = 2
class SensorType(Enum):
HTTP = 1
XML = 2
class DeviceType(Enum):
PRINTER = 1
DEVICE = 2
# XML Utils
def indent(elem, level=0):
i = "\n" + level * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def writeXMLFile(root, **kwargs):
filename = kwargs.get('filename', str(uuid4()) + '.odt')
indent(root)
document = ET.ElementTree(root)
document.write(filename, encoding='utf-8', xml_declaration=True)
print('Template saved: {0}'.format(filename))
# PRTG Primitive Nodes
def createCheckNode(checkType):
if checkType is CheckType.PING:
node = ET.Element('check')
node.set('id', 'ping')
node.set('meta', 'ping')
return node
elif checkType is CheckType.DOTNET:
node = ET.Element('check')
node.set('id', 'dotnet40')
node.set('meta', 'dotnet')
node.set('requires', 'ping')
metadata = ET.SubElement(node, 'metadata')
requireddotnetversion = ET.SubElement(
metadata, 'requireddotnetversion')
requireddotnetversion.text = '40'
return node
else:
raise TypeError
def createSensorNode(sensorType, displayName, tags, url, **kwargs):
xmlNode = kwargs.get('xml_node', '')
node = ET.Element('create')
node.set('id', str(uuid4()))
node.set('displayname', displayName)
createData = ET.SubElement(node, 'createdata')
ET.SubElement(createData, 'tags').text = tags
ET.SubElement(createData, 'priority').text = '3'
if sensorType is SensorType.HTTP:
node.set('requires', 'ping')
node.set('kind', 'http')
ET.SubElement(createData, 'httpurl').text = url
elif sensorType is SensorType.XML:
node.set('requires', 'dotnet40')
node.set('kind', 'ptfhttpxmlrestvalue')
ET.SubElement(createData, 'xmlurl').text = url
ET.SubElement(createData, 'xmlnode').text = xmlNode
else:
return None
return node
def createDeviceTemplateRoot(**kwargs):
root = ET.Element('devicetemplate')
root.set('id', kwargs.get('id', 'custom'))
root.set('name', kwargs.get('name', ''))
root.set('priority', kwargs.get('priority', '1'))
root.set('adddevicename', kwargs.get('name', ''))
return root
# PRTG Composite Nodes
def buildServerTemplate(server):
print('Creating PRTG PaperCut Server Template...')
root = createDeviceTemplateRoot(id='pc-server',
name='PaperCut Server')
root.append(createCheckNode(CheckType.PING))
root.append(createCheckNode(CheckType.DOTNET))
root.append(createSensorNode(SensorType.HTTP,
'Overall Server Health',
'server',
server.getServerHealthNodeUrl()))
root.append(createSensorNode(SensorType.XML,
'Held Jobs',
'held_jobs',
server.getHeldJobsNodeUrl(),
xml_node='heldJobsCount'))
root.append(createSensorNode(SensorType.XML,
'Recent Pages Printed (1m)',
'pages_printed',
server.getRecentPagesNodeUrl(),
xml_node='recentPagesCount'))
root.append(createSensorNode(SensorType.XML,
'Recent Errors (10m)',
'recent_errors',
server.getRecentErrorsNodeUrl(),
xml_node='recentErrorsCount'))
root.append(createSensorNode(SensorType.XML,
'Recent Warnings (10m)',
'recent_warnings',
server.getRecentWarningsNodeUrl(),
xml_node='recentWarningsCount'))
return root
def buildPrinterTemplate(printerCollection, **kwargs):
id = kwargs.get('id', 'custom')
name = kwargs.get('name', '')
root = createDeviceTemplateRoot(id=id,
name=name)
root.append(createCheckNode(CheckType.PING))
for printer in printerCollection:
root.append(printer.createXMLNode())
return root
# Filters
def filterCSV(csvReader, predicateList):
for row in csvReader:
if all(f(row) for f in predicateList):
yield row
def namePredicate(name):
return lambda row: name in row[PRINTER_NAME_COLUMN]
def serverPredicate(server, deviceType):
if deviceType is DeviceType.DEVICE:
return lambda row: True
if deviceType is DeviceType.PRINTER:
return lambda row: server in row[PRINTER_SERVER_COLUMN]
return TypeError
def locationPredicate(location, deviceType):
if deviceType is DeviceType.DEVICE:
return lambda row: location in row[DEVICE_LOCATION_COLUMN]
if deviceType is DeviceType.PRINTER:
return lambda row: location in row[PRINTER_LOCATION_COLUMN]
return TypeError
def consolidatePredicates(args, deviceType):
predicateList = []
if args.server is not None:
predicateList.append(serverPredicate(str(args.server), deviceType))
if args.location is not None:
predicateList.append(locationPredicate(str(args.location), deviceType))
if args.name is not None:
predicateList.append(namePredicate(str(args.name)))
return predicateList
# Build Printer Data
def createPrinterCollection(source, max):
printerCollection = []
for row in islice(source, max):
printerCollection.append(Printer(row[PRINTER_NAME_COLUMN],
row[PRINTER_LOCATION_COLUMN],
row[PRINTER_URL_COLUMN],
DeviceType.PRINTER,
printer_server=row[PRINTER_SERVER_COLUMN]))
return printerCollection
def createDeviceCollection(source, max):
deviceCollection = []
for row in islice(source, max):
deviceCollection.append(Printer(row[DEVICE_NAME_COLUMN],
row[DEVICE_LOCATION_COLUMN],
row[DEVICE_URL_COLUMN],
DeviceType.DEVICE))
return deviceCollection
class PapercutServer:
def __init__(self, scheme, address, port, authkey):
self.scheme = scheme
self.address = address
self.port = port
self.authkey = authkey
# API Urls
def getServerHealthNodeUrl(self):
return '{0}://{1}:{2}/api/health?Authorization={3}'\
.format(self.scheme, self.address, self.port, self.authkey)
def getHeldJobsNodeUrl(self):
return '{0}://{1}:{2}/api/stats/held-jobs-count?Authorization={3}'\
.format(self.scheme, self.address, self.port, self.authkey)
def getRecentPagesNodeUrl(self):
return '{0}://{1}:{2}/api/stats/recent-pages-count?minutes=1&Authorization={3}'\
.format(self.scheme, self.address, self.port, self.authkey)
def getRecentErrorsNodeUrl(self):
return '{0}://{1}:{2}/api/stats/recent-errors-count?minutes=10&Authorization={3}'\
.format(self.scheme, self.address, self.port, self.authkey)
def getRecentWarningsNodeUrl(self):
return '{0}://{1}:{2}/api/stats/recent-warnings-count?minutes=1&Authorization={3}'\
.format(self.scheme, self.address, self.port, self.authkey)
# CSV Urls
def getPrintersCSVUrl(self):
return '{0}://{1}:{2}/api/health/printers/urls?Authorization={3}'\
.format(self.scheme, self.address, self.port, self.authkey)
def getDevicesCSVUrl(self):
return '{0}://{1}:{2}/api/health/devices/urls?Authorization={3}'\
.format(self.scheme, self.address, self.port, self.authkey)
class Printer:
def __init__(self, printerName, printerLocation, statusUrl, deviceType, **kwargs):
self.printerServer = kwargs.get('printer_server', '')
self.printerName = printerName
self.printerLocation = printerLocation
self.statusUrl = statusUrl
self.deviceType = deviceType
def createXMLNode(self):
if self.deviceType is DeviceType.PRINTER:
displayName = '{0}/{1}'.format(self.printerServer,
self.printerName)
tags = 'printer'
elif self.deviceType is DeviceType.DEVICE:
displayName = '{0}'.format(self.printerName)
tags = 'device'
else:
raise TypeError
return createSensorNode(SensorType.HTTP,
displayName,
tags,
self.statusUrl)
def main():
scheme = pcUrl.scheme
address = pcUrl.hostname
port = '80'
if scheme == 'https':
https_handler = HTTPSHandler(
context=ssl.SSLContext(ssl.PROTOCOL_SSLv23))
install_opener(build_opener(https_handler))
if pcUrl.port is not None:
port = pcUrl.port
authkey = re.split('[=&]', pcUrl.query)[-1]
server = PapercutServer(scheme, address, port, authkey)
print('Connecting to PaperCut Installation at {0}://{1}:{2}'
.format(server.scheme, server.address, server.port))
print('Filtering:\n\tServer:\t\t{0}\n\tLocation:\t{1}\n\tName:\t\t{2}'
.format(args.server, args.location, args.name))
# Printers
printerRequest = Request(server.getPrintersCSVUrl())
try:
printerResponse = urlopen(printerRequest)
except URLError as e:
print(e)
return e
printerList = reader(iterdecode(printerResponse, 'utf-8'))
next(printerList, None)
print('Reading Printer URLs...')
printerPredicateList = consolidatePredicates(args, DeviceType.PRINTER)
if len(printerPredicateList) is not 0:
printerCollection = createPrinterCollection(
filterCSV(printerList, printerPredicateList), args.limit)
else:
printerCollection = createPrinterCollection(printerList, args.limit)
if len(printerCollection) is 0:
print('No printers found.')
else:
printerTemplate = buildPrinterTemplate(printerCollection,
id='pc-printers',
name='PaperCut Printers')
writeXMLFile(printerTemplate, filename='PaperCut Printers.odt')
# Devices
deviceRequest = Request(server.getDevicesCSVUrl())
try:
deviceResponse = urlopen(deviceRequest)
except URLError as e:
print(e)
return e
deviceList = reader(iterdecode(deviceResponse, 'utf-8'))
next(deviceList, None)
print('Reading Device URLs...')
devicePredicateList = consolidatePredicates(args, DeviceType.DEVICE)
if len(devicePredicateList) is not 0:
deviceCollection = createDeviceCollection(
filterCSV(deviceList, devicePredicateList), args.limit)
else:
deviceCollection = createDeviceCollection(deviceList, args.limit)
if len(deviceCollection) is 0:
print('No devices found.')
else:
deviceTemplate = buildPrinterTemplate(deviceCollection,
id='pc-devices',
name='PaperCut Printers')
writeXMLFile(deviceTemplate, filename='PaperCut Devices.odt')
# Server
serverTemplate = buildServerTemplate(server)
writeXMLFile(serverTemplate, filename='PaperCut Server.odt')
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Generate PaperCut Printer and Device status templates for PRTG')
parser.add_argument('address',
help='The GET query URL for your PaperCut Server Health API' +
' (See Options->Advanced->System Health Monitoring)' +
' example: http://203.0.113.0:9191/api/health/?Authorization=authKey1234')
parser.add_argument('-n', '--name', default=None,
help='Filter by name (default none)')
parser.add_argument('-lo', '--location', default=None,
help='Filter by location (default none)')
parser.add_argument('-s', '--server', default=None,
help='Filter by server (default none)')
parser.add_argument('-li', '--limit', default=250, type=int,
help='Maximum number of printers to include in template (default 250)')
args = parser.parse_args()
pcUrl = urlparse(args.address)
main()