-
Notifications
You must be signed in to change notification settings - Fork 39
/
template.py
executable file
·141 lines (126 loc) · 4.6 KB
/
template.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
#!/usr/bin/env python3
# coding=utf-8
# [% VIM_TAGS %]
#
# Author: Hari Sekhon
# Date: [% DATE # 2008-10-20 16:18:55 +0100 (Mon, 20 Oct 2008) %]
#
# [% URL %]
#
# [% LICENSE %]
#
# [% MESSAGE %]
#
# [% LINKEDIN %]
#
"""
TODO
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
#import logging
import os
#import re
import sys
#import time
import traceback
#try:
# from bs4 import BeautifulSoup
#except ImportError:
# print(traceback.format_exc(), end='')
# sys.exit(4)
srcdir = os.path.abspath(os.path.dirname(__file__))
libdir = os.path.join(srcdir, 'pylib')
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import log
#from harisekhon.utils import CriticalError, UnknownError
from harisekhon.utils import validate_host, validate_port, validate_user, validate_password
from harisekhon.utils import isStr
from harisekhon import CLI
from harisekhon import RestNagiosPlugin
except ImportError as _:
print(traceback.format_exc(), end='')
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.1'
class [% NAME %](RestNagiosPlugin):
def __init__(self):
# Python 2.x
super([% NAME %], self).__init__()
# Python 3.x
# super().__init__()
#self.host = None
#self.port = None
#self.user = None
#self.password = None
#self.protocol = 'http'
#self.request = RequestHandler()
self.name =
self.default_port = 80
self.path = '/'
#self.auth = False
self.json = True
self.msg = 'Msg not defined yet'
def add_options(self):
super([% NAME %], self).add_options()
# TODO: fill in hostoption name and default port
#self.add_hostoption(name='', default_host='localhost', default_port=80)
#self.add_useroption(name='', default_user='admin')
#self.add_opt('-S', '--ssl', action='store_true', help='Use SSL')
#self.add_opt('-f', '--file', dest='file', metavar='<file>',
# help='Input file')
def process_options(self):
super([% NAME %], self).process_options()
#self.no_args()
#self.host = self.get_opt('host')
#self.port = self.get_opt('port')
#self.user = self.get_opt('user')
#self.password = self.get_opt('password')
#validate_host(self.host)
#validate_port(self.port)
#validate_user(self.user)
#validate_password(self.password)
#if self.get_opt('ssl'):
# self.protocol = 'https'
#filename = self.get_opt('file')
#if not filename:
# self.usage('--file not defined')
# def run(self):
# url = '{protocol}://{host}:{port}/...'.format(protocol=self.protocol, host=self.host, port=self.port)
# start_time = time.time()
# req = self.request.get(url)
# query_time = time.time() - start_time
# soup = BeautifulSoup(req.content, 'html.parser')
# if log.isEnabledFor(logging.DEBUG):
# log.debug("BeautifulSoup prettified:\n{0}\n{1}".format(soup.prettify(), '='*80))
# # TODO: XXX: soup.find() can return None - do not chain calls - must test each call 'is not None'
# # link = soup.find('p')[3]
# # link = soup.find('th', text='Uptime:')
# # link = soup.find_next_sibling('th', text='Uptime:')
#
# # link = soup.find('th', text=re.compile('Uptime:?', re.I))
# # if link is None:
# # raise UnknownError('failed to find tag')
# # link = link.find_next_sibling()
# # if link is None:
# # raise UnknownError('failed to find tag (next sibling tag not found)')
# # _ = link.get_text()
# # shorter to just catch NoneType attribute error when tag not found and returns None
# try:
# uptime = soup.find('th', text=re.compile('Uptime:?', re.I)).find_next_sibling().get_text()
# version = soup.find('th', text=re.compile('Version:?', re.I)).find_next_sibling().get_text()
# except (AttributeError, TypeError):
# #raise UnknownError('failed to find parse output')
# qquit('UNKNOWN', 'failed to parse output')
# if not _ or not isStr(_) or not re.search(r'...', _):
# #raise UnknownError('format not recognized: {0}'.format(_))
# qquit('UNKNOWN', 'format not recognized: {0}'.format(_))
# self.msg += ' | query_time={0:f}s'.format(query_time)
def parse_json(self, json_data):
_ = json_data['beans'][0]
if __name__ == '__main__':
[% NAME %]().main()