Skip to content

Commit

Permalink
minimal refactoring + checkmk api fetch_url
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri Wahl committed Sep 2, 2024
1 parent d0ae96a commit 811d4cf
Show file tree
Hide file tree
Showing 25 changed files with 501 additions and 532 deletions.
2 changes: 1 addition & 1 deletion Nagstamon/Objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def get_hash(self):
class Result(object):

"""
multi purpose result object, used in Servers.Generic.FetchURL()
multi purpose result object, used in Servers.Generic.fetch_url()
"""
result = ''
error = ''
Expand Down
42 changes: 21 additions & 21 deletions Nagstamon/QUI/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ def open_url(self):
url = url.replace('$MONITOR-CGI$', self.server.monitor_cgi_url)

if conf.debug_mode:
self.server.Debug(server=self.server.get_name(), debug='Open {0} web page {1}'.format(self.url_type, url))
self.server.debug(server=self.server.get_name(), debug='Open {0} web page {1}'.format(self.url_type, url))

# use Python method to open browser
webbrowser_open(url)
Expand Down Expand Up @@ -2074,7 +2074,7 @@ def refresh(self):

for server in get_enabled_servers():
if conf.debug_mode:
server.Debug(server=server.name, debug='Refreshing all hosts and services')
server.debug(server=server.name, debug='Refreshing all hosts and services')

# manipulate server thread counter so get_status loop will refresh when next looking
# at thread counter
Expand Down Expand Up @@ -2394,7 +2394,7 @@ def execute_action(self, server_name, custom_action_string):
execute custom action
"""
if conf.debug_mode:
servers[server_name].Debug(debug='NOTIFICATION: ' + custom_action_string)
servers[server_name].debug(debug='NOTIFICATION: ' + custom_action_string)
subprocess.Popen(custom_action_string, shell=True)

def get_worst_notification_status(self):
Expand Down Expand Up @@ -3628,7 +3628,7 @@ def action_menu_custom_response(self, action):

# get data to send to action
server = self.server.get_name()
address = self.server.GetHost(miserable_host).result
address = self.server.get_host(miserable_host).result
monitor = self.server.monitor_url
monitor_cgi = self.server.monitor_cgi_url
username = self.server.username
Expand Down Expand Up @@ -3792,7 +3792,7 @@ def action_archive_event(self):
'host': host,
'service': service,
'status-info': status,
'address': self.server.GetHost(host).result,
'address': self.server.get_host(host).result,
'monitor': self.server.monitor_url,
'monitor-cgi': self.server.monitor_cgi_url,
'username': self.server.username,
Expand Down Expand Up @@ -4063,7 +4063,7 @@ def get_status(self):
# reflect status retrieval attempt on server vbox label
self.change_label_status.emit('Refreshing...', '')

status = self.server.GetStatus()
status = self.server.get_status()

# all is OK if no error info came back
if self.server.status_description == '' and \
Expand Down Expand Up @@ -4291,9 +4291,9 @@ def recheck(self, info_dict):
if conf.debug_mode:
# host
if info_dict['service'] == '':
self.server.Debug(server=self.server.name, debug='Rechecking host {0}'.format(info_dict['host']))
self.server.debug(server=self.server.name, debug='Rechecking host {0}'.format(info_dict['host']))
else:
self.server.Debug(server=self.server.name,
self.server.debug(server=self.server.name,
debug='Rechecking service {0} on host {1}'.format(info_dict['service'],
info_dict['host']))

Expand All @@ -4312,22 +4312,22 @@ def recheck_all(self):
# change label of server vbox
self.change_label_status.emit('Rechecking all...', '')
if conf.debug_mode:
self.server.Debug(server=self.server.name, debug='Start rechecking all')
self.server.debug(server=self.server.name, debug='Start rechecking all')
# special treatment for Checkmk Multisite because there is only one URL call necessary
if self.server.type != 'Checkmk Multisite':
# make a copy to preserve hosts/service to recheck - just in case something changes meanwhile
nagitems_filtered = deepcopy(self.server.nagitems_filtered)
for status in nagitems_filtered['hosts'].items():
for host in status[1]:
if conf.debug_mode:
self.server.Debug(server=self.server.name,
self.server.debug(server=self.server.name,
debug='Rechecking host {0}'.format(host.name))
# call server recheck method
self.server.set_recheck({'host': host.name, 'service': ''})
for status in nagitems_filtered['services'].items():
for service in status[1]:
if conf.debug_mode:
self.server.Debug(server=self.server.name,
self.server.debug(server=self.server.name,
debug='Rechecking service {0} on host {1}'.format(service.name,
service.host))
# call server recheck method
Expand All @@ -4342,7 +4342,7 @@ def recheck_all(self):
self.restore_label_status.emit()
else:
if conf.debug_mode:
self.server.Debug(server=self.server.name, debug='Already rechecking all')
self.server.debug(server=self.server.name, debug='Already rechecking all')

@Slot(str, str)
def get_start_end(self, server_name, host):
Expand Down Expand Up @@ -4409,13 +4409,13 @@ def execute_action(self, action, info):
if action['type'] == 'browser':
# debug
if conf.debug_mode is True:
self.server.Debug(server=self.server.name, host=info['host'], service=info['service'],
self.server.debug(server=self.server.name, host=info['host'], service=info['service'],
debug='ACTION: BROWSER ' + string)
webbrowser_open(string)
elif action['type'] == 'command':
# debug
if conf.debug_mode is True:
self.server.Debug(server=self.server.name, host=info['host'], service=info['service'],
self.server.debug(server=self.server.name, host=info['host'], service=info['service'],
debug='ACTION: COMMAND ' + string)
subprocess.Popen(string, shell=True)
elif action['type'] == 'url':
Expand All @@ -4428,18 +4428,18 @@ def execute_action(self, action, info):
string = self._URLify(string)
# debug
if conf.debug_mode is True:
self.server.Debug(server=self.server.name, host=info['host'], service=info['service'],
self.server.debug(server=self.server.name, host=info['host'], service=info['service'],
debug='ACTION: URL in background ' + string)
servers[info['server']].FetchURL(string)
servers[info['server']].fetch_url(string)
# used for example by Op5Monitor.py
elif action['type'] == 'url-post':
# make string ready for URL
string = self._URLify(string)
# debug
if conf.debug_mode is True:
self.server.Debug(server=self.server.name, host=info['host'], service=info['service'],
self.server.debug(server=self.server.name, host=info['host'], service=info['service'],
debug='ACTION: URL-POST in background ' + string)
servers[info['server']].FetchURL(string, cgi_data=cgi_data, multipart=True)
servers[info['server']].fetch_url(string, cgi_data=cgi_data, multipart=True)

if action['recheck']:
self.recheck(info)
Expand Down Expand Up @@ -7011,9 +7011,9 @@ def check(self):
message = 'Cannot reach version check at <a href={0}>{0}</<a>.'.format(
f'https://{download_server}{AppInfo.VERSION_PATH}')
# retrieve VERSION_URL without auth information
response = server.FetchURL(f'https://{download_server}{AppInfo.VERSION_PATH}',
giveback='raw',
no_auth=True)
response = server.fetch_url(f'https://{download_server}{AppInfo.VERSION_PATH}',
giveback='raw',
no_auth=True)
# stop searching the available download URLs
if response.error == "" and \
not response.result.startswith('<') and \
Expand Down
12 changes: 6 additions & 6 deletions Nagstamon/Servers/Alertmanager/alertmanagerserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ def _get_status(self):
# get all alerts from the API server
try:
if self.alertmanager_filter != '':
result = self.FetchURL(self.monitor_url + self.API_PATH_ALERTS + self.API_FILTERS
result = self.fetch_url(self.monitor_url + self.API_PATH_ALERTS + self.API_FILTERS
+ self.alertmanager_filter, giveback="raw")
else:
result = self.FetchURL(self.monitor_url + self.API_PATH_ALERTS,
giveback="raw")
result = self.fetch_url(self.monitor_url + self.API_PATH_ALERTS,
giveback="raw")

if result.status_code == 200:
log.debug("received status code '%s' with this content in result.result: \n\
Expand Down Expand Up @@ -258,7 +258,7 @@ def _get_status(self):
except Exception as the_exception:
# set checking flag back to False
self.isChecking = False
result, error = self.Error(sys.exc_info())
result, error = self.error(sys.exc_info())
log.exception(the_exception)
return Result(result=result, error=error)

Expand Down Expand Up @@ -353,6 +353,6 @@ def _set_acknowledge(self, host, service, author, comment, sticky, notify, persi
cgi_data["createdBy"] = author or "Nagstamon"
cgi_data = json.dumps(cgi_data)

result = self.FetchURL(self.monitor_url + self.API_PATH_SILENCES, giveback="raw",
cgi_data=cgi_data)
result = self.fetch_url(self.monitor_url + self.API_PATH_SILENCES, giveback="raw",
cgi_data=cgi_data)
return result
Loading

0 comments on commit 811d4cf

Please sign in to comment.