Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Release 0.2
Browse files Browse the repository at this point in the history
- Standardized Shebang lines and option descriptions
- Changed program version to 0.2
- Implemented safer authentication mechanism for
``satprep_schedule_downtime.py`` (Issue #17)
- Implemented unschedule function for
``satprep_schedule_downtime.py``(Issue #18)
- Moved schedule downtime code into ``satprep_shared`` to make it
easier to integrate with other scripts
- Fixed bug with detecting patches/errata requiring reboot in
``satprep_schedule_downtime.py``
- Renamed some options and removed authentication using options in
``satprep_schedule_downtime.py``
  • Loading branch information
Christian Stankowic authored and Christian Stankowic committed Nov 30, 2014
1 parent 374070d commit ef7fa39
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 100 deletions.
16 changes: 3 additions & 13 deletions satprep_diff.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# satprep_diff.py - a script for creating patch
# diff reports
Expand Down Expand Up @@ -32,42 +33,31 @@ class LaTeXTemplate(string.Template):
desc='''%prog is used to create patch diff reports of systems managed with Spacewalk, Red Hat Satellite and SUSE Manager. The script needs TeXlive/LaTeX to create PDF reports. Defining your own templates is possible - the default template needs to be located in the same directory like this script.
Checkout the GitHub page for updates: https://github.com/stdevel/satprep'''
parser = OptionParser(description=desc,version="%prog version 0.1")
parser = OptionParser(description=desc,version="%prog version 0.2")

#-q / --quiet
parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout")

#-d / --debug
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")

#-t / --template
parser.add_option("-t", "--template", dest="template", default="default", metavar="FILE", help="defines the template which is used to generate the report")

#-o / --output
parser.add_option("-o", "--output", action="store", type="string", dest="output", default="foobar", help="define report filename. (default: errata-diff-report-Ymd.csv)", metavar="FILE")

#-u / --use-delta-from
#parser.add_option("-u", "--use-delta-from", action="store", type="string", dest="deltafile", default="", metavar="FILE", help="defines previously created delta file - useful if you don't want to re-create the delta")

#-n / --no-host-reports
parser.add_option("-n", "--no-host-reports", action="store_true", default=False, dest="noHostReports", help="only create delta CSV report and skip creating host reports")

#-x / --preserve-tex
parser.add_option("-x", "--preserve-tex", action="store_true", default=False, dest="preserveTex", help="keeps the TeX files after creating the PDF reports (default: no)")

#-p / --page-orientation
parser.add_option("-p", "--page-orientation", action="store", type="choice", dest="pageOrientation", default="landscape", metavar="[landscape|potrait]", choices=["landscape","potrait"], help="defines the orientation of the PDF report (default: landscape)")

#-i / --image
parser.add_option("-i", "--image", action="store", type="string", dest="logoImage", metavar="FILE", help="defines a different company logo")

#-c / --csv
#parser.add_option("-c", "--csv", action="store", type="string", dest="csvReport", metavar="FILE", help="uses a pre-existing CSV delta report")
#TODO: implement

#-f / --footer
parser.add_option("-f", "--footer", action="store", type="string", default="", dest="footer", metavar="STRING", help="changes footer text")

#-b / --pdflatex-binary
parser.add_option("-b", "--pdflatex-binary", action="store", type="string", dest="pathPdflatex", default="/usr/bin/pdflatex", metavar="PATH", help="location for the pdflatex binary")

Expand Down
33 changes: 17 additions & 16 deletions satprep_install_custominfos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# satprep_install_custominfos.py - a script for creating
# custom information that can be automatically inserted in
Expand Down Expand Up @@ -98,21 +99,21 @@ def parse_options(args=None):
If you're not defining variables or an authfile you will be prompted to enter your login information.
Checkout the GitHub page for updates: https://github.com/stdevel/satprep'''
parser = OptionParser(description=desc, version="%prog version 0.1")
parser.add_option("-a", "--authfile", dest="authfile", metavar="FILE",
default="", help="defines an auth file to use instead of shell variables")
parser.add_option("-s", "--server", dest="server", metavar="SERVER",
default="localhost", help="defines the server to use")
parser.add_option("-q", "--quiet", action="store_false", dest="verbose",
default=True, help="don't print status messages to stdout")
parser.add_option("-d", "--debug", dest="debug", default=False,
action="store_true", help="enable debugging outputs")
parser.add_option("-n", "--dry-run", action="store_true", dest="dryrun",
default=False, help="only simulates the creation of custom keys")
parser.add_option("-f", "--force", action="store_true", dest="force",
default=False, help="overwrites previously created custom keys with the same name")
parser.add_option("-u", "--uninstall", action="store_true", dest="uninstall",
default=False, help="removes previously installed custom info keys")
parser = OptionParser(description=desc, version="%prog version 0.2")
#-a / --authfile
parser.add_option("-a", "--authfile", dest="authfile", metavar="FILE", default="", help="defines an auth file to use instead of shell variables")
#-s / --server
parser.add_option("-s", "--server", dest="server", metavar="SERVER", default="localhost", help="defines the server to use")
#-q / --quiet
parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout")
#-d / --debug
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")
#-n / --dry-run
parser.add_option("-n", "--dry-run", action="store_true", dest="dryrun", default=False, help="only simulates the creation of custom keys")
#-f / --force
parser.add_option("-f", "--force", action="store_true", dest="force", default=False, help="overwrites previously created custom keys with the same name")
#-u / --uninstall
parser.add_option("-u", "--uninstall", action="store_true", dest="uninstall", default=False, help="removes previously installed custom info keys")

(options, args) = parser.parse_args(args)

Expand Down
61 changes: 22 additions & 39 deletions satprep_schedule_downtime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# satprep_schedule_downtime.py - a script for scheduling
# downtimes for hosts monitored by Nagios/Icinga/Thruk/Shinken
Expand All @@ -11,17 +12,22 @@
import logging
import sys
from optparse import OptionParser
import requests
from requests.auth import HTTPBasicAuth
import time
from datetime import datetime, timedelta
import csv
from satprep_shared import schedule_downtime, get_credentials

#set logger
LOGGER = logging.getLogger('satprep_schedule_downtime')
targetHosts=[]

def setDowntime():
#stop if no hosts affected
if len(targetHosts) == 0:
LOGGER.info("Nothing to do, going home!")
exit(0)

#get monitoring credentials
(monUsername, monPassword) = get_credentials(options.authfile)

#set downtime for affected hosts
for host in targetHosts:
if options.dryrun == True:
Expand All @@ -36,32 +42,9 @@ def setDowntime():
myHeaders = {'User-Agent': options.userAgent}
else:
myHeaders = {'User-Agent': 'satprep Toolkit (https://github.com/stdevel/satprep)'}
LOGGER.debug("Setting headers: {0}".format(myHeaders))

#setup start and end time for downtime
current_time=time.strftime("%Y-%m-%d %H:%M:%S")
end_time=format(datetime.now() + timedelta(hours=int(options.hours)), '%Y-%m-%d %H:%M:%S')
LOGGER.debug("current_time: {0}".format(current_time))
LOGGER.debug("end_time: {0}".format(end_time))

#setup payload
payload = {'cmd_typ': '55', 'cmd_mod': '2', 'host': host, 'com_data': options.comment, 'trigger': '0', 'fixed': '1', 'hours': options.hours, 'minutes': '0', 'start_time': current_time, 'end_time': end_time, 'btnSubmit': 'Commit', 'com_author': options.monUsername, 'childoptions': '0'}
LOGGER.debug("payload: {0}".format(payload))

#setup HTTP session
s = requests.Session()
if options.noAuth == False: s.auth = HTTPBasicAuth(options.monUsername, options.monPassword)

#send POST request
r = s.post(options.URL+"/cgi-bin/cmd.cgi", data=payload, headers=myHeaders)
LOGGER.debug("result: {0}".format(r.text))

#check whether request was successful
if r.status_code != 200:
LOGGER.error("ERROR: Got HTTP status code " + str(r.status_code) + " instead of 200 while scheduling downtime for host '" + host + "'. Check URL and logon credentials!")
else:
if "error" in r.text.lower(): LOGGER.error("ERROR: unable to schedule downtime for host '" + host + "' - please run again with -d / --debug and check HTML output!")
else: print "Successfully scheduled downtime for host '" + host + "'"
#(un)schedule downtime
result = schedule_downtime(options.URL, monUsername, monPassword, host, options.hours, options.comment, options.userAgent, options.noAuth, options.unschedule)



Expand Down Expand Up @@ -92,7 +75,7 @@ def readFile(file):
targetHosts.append(row[repcols["hostname"]])
else:
#add host if reboot required and monitoring flag set
if repcols["system_monitoring"] < 666 and row[repcols["system_monitoring"]] == "1" and repcols["errata_reboot"] < 666 and row[repcols["errata_reboot"]] != "": targetHosts.append(row[repcols["hostname"]])
if repcols["system_monitoring"] < 666 and row[repcols["system_monitoring"]] == "1" and repcols["errata_reboot"] < 666 and row[repcols["errata_reboot"]] == "1": targetHosts.append(row[repcols["hostname"]])
#remove duplicates and 'hostname' line
targetHosts = sorted(set(targetHosts))
if "hostname" in targetHosts: targetHosts.remove("hostname")
Expand All @@ -117,27 +100,27 @@ def parse_options(args=None):

# define description, version and load parser
desc = '''%prog is used to schedule downtimes for create the custom information keys used by satprep_snapshot.py to gather more detailed system information. You only need to create those keys once (e.g. before using the first time or after a re-installation of Satellite). Login credentials are assigned using the following shell variables:'''
parser = OptionParser(description=desc, version="%prog version 0.1")
#-U / --url
parser.add_option("-U", "--url", dest="URL", metavar="URL", default="http://localhost/icinga", help="defines the Nagios/Icinga/Thruk/Shinken URL to use (default: http://localhost/icinga)")
#-u / --username
parser.add_option("-u", "--username", action="store", dest="monUsername", metavar="USERNAME", help="defines the username used for the monitoring user-interface")
#-p / --password
parser.add_option("-p", "--password", action="store", dest="monPassword", metavar="PASSWORD", help="defines the password")
parser = OptionParser(description=desc, version="%prog version 0.2")
#-a / --authfile
parser.add_option("-a", "--authfile", dest="authfile", metavar="FILE", default="", help="defines an auth file to use instead of shell variables")
#-u / --url
parser.add_option("-u", "--url", dest="URL", metavar="URL", default="http://localhost/icinga", help="defines the Nagios/Icinga/Thruk/Shinken URL to use (default: http://localhost/icinga)")
#-t / --hours
parser.add_option("-t", "--hours", action="store", dest="hours", default="2", metavar="HOURS", help="sets the time period in hours hosts should be scheduled for downtime (default: 2)")
#-c / --comment
parser.add_option("-c", "--comment", action="store", dest="comment", default="System maintenance scheduled by satprep", metavar="COMMENT", help="defines a comment for scheduled downtime (default: 'System maintenance scheduled by satprep')")
#-x / --no-auth
parser.add_option("-x", "--no-auth", action="store_true", default=False, dest="noAuth", help="disables HTTP basic auth (often used with Nagios/Icinga and OMD) (default: no)")
#-a / --user-agent
parser.add_option("-a", "--user-agent", action="store", default="", metavar="AGENT", dest="userAgent", help="sets a custom HTTP user agent")
#-A / --user-agent
parser.add_option("-A", "--user-agent", action="store", default="", metavar="AGENT", dest="userAgent", help="sets a custom HTTP user agent")
#-d / --debug
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")
#-f / --no-intelligence
parser.add_option("-f", "--no-intelligence", dest="noIntelligence", action="store_true", default=False, help="disables checking for patches requiring reboot, simply schedules downtime for all hosts mentioned in the CSV report (default: no)")
#-n / --dry-run
parser.add_option("-n", "--dry-run", action="store_true", dest="dryrun", default=False, help="only simulates scheduling downtimes")
#-U / --unschedule
parser.add_option("-U", "--unschedule", dest="unschedule", action="store_true", default=False, help="unschedules downtimes instead of scheduling them (default: no)")

(options, args) = parser.parse_args(args)

Expand Down
55 changes: 53 additions & 2 deletions satprep_shared.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#! /usr/bin/env python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getpass
import logging
import os
import stat
import sys
import requests
from requests.auth import HTTPBasicAuth
import time
from datetime import datetime, timedelta

LOGGER = LOGGER = logging.getLogger('satprep-shared')
LOGGER = logging.getLogger('satprep-shared')

SUPPORTED_API_LEVELS = ["11.1", "12", "13", "13.0", "14", "14.0", "15", "15.0"]

Expand All @@ -16,6 +21,7 @@ class APILevelNotSupportedException(Exception):


def check_if_api_is_supported(client):
#check whether API is supported
api_level = client.api.getVersion()
if api_level not in SUPPORTED_API_LEVELS:
raise APILevelNotSupportedException(
Expand All @@ -26,6 +32,7 @@ def check_if_api_is_supported(client):
LOGGER.info("INFO: supported API version (" + api_level + ") found.")

def get_credentials(input_file=None):
#retrieve credentials
if input_file:
LOGGER.debug("DEBUG: using authfile")
try:
Expand Down Expand Up @@ -53,3 +60,47 @@ def get_credentials(input_file=None):
s_username = raw_input("Username: ")
s_password = getpass.getpass("Password: ")
return (s_username, s_password)

def schedule_downtime(url, monUsername, monPassword, host, hours, comment, agent="", noAuth=False, unschedule=False):
#schedule downtime

#setup headers
if len(agent) > 0:
myHeaders = {'User-Agent': agent}
else:
myHeaders = {'User-Agent': 'satprep Toolkit (https://github.com/stdevel/satprep)'}
LOGGER.debug("Setting headers: {0}".format(myHeaders))

#setup start and end time for downtime
current_time=time.strftime("%Y-%m-%d %H:%M:%S")
end_time=format(datetime.now() + timedelta(hours=int(hours)), '%Y-%m-%d %H:%M:%S')
LOGGER.debug("current_time: {0}".format(current_time))
LOGGER.debug("end_time: {0}".format(end_time))

#setup payload
if unschedule:
payload = {'cmd_typ': '171', 'cmd_mod': '2', 'host': host, 'btnSubmit': 'Commit'}
else:
payload = {'cmd_typ': '55', 'cmd_mod': '2', 'host': host, 'com_data': comment, 'trigger': '0', 'fixed': '1', 'hours': hours, 'minutes': '0', 'start_time': current_time, 'end_time': end_time, 'btnSubmit': 'Commit', 'com_author': monUsername, 'childoptions': '0'}
LOGGER.debug("payload: {0}".format(payload))

#setup HTTP session
s = requests.Session()
if noAuth == False: s.auth = HTTPBasicAuth(monUsername, monPassword)

#send POST request
r = s.post(url+"/cgi-bin/cmd.cgi", data=payload, headers=myHeaders)
try:
LOGGER.debug("result: {0}".format(r.text))
except:
LOGGER.debug("result: none - check URL/authentification method!")


#check whether request was successful
if r.status_code != 200:
LOGGER.error("ERROR: Got HTTP status code " + str(r.status_code) + " instead of 200 while scheduling downtime for host '" + host + "'. Check URL and logon credentials!")
return False
else:
if "error" in r.text.lower(): LOGGER.error("ERROR: unable to (un)schedule downtime for host '" + host + "' - please run again with -d / --debug and check HTML output! (does this host exist?!)")
else: print "Successfully (un)scheduled downtime for host '" + host + "'"
return True
50 changes: 20 additions & 30 deletions satprep_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# satprep_snapshot.py - a script for creating a snapshot
# report of available errata available to systems managed
Expand Down Expand Up @@ -48,35 +49,24 @@ def parse_options(args=None):
If you're not defining variables or an authfile you will be prompted to enter your login information.
Checkout the GitHub page for updates: https://github.com/stdevel/satprep'''
parser = OptionParser(description=desc, version="%prog version 0.1")

parser.add_option("-a", "--authfile", dest="authfile", metavar="FILE",
default="",
help="defines an auth file to use instead of shell variables")
parser.add_option("-s", "--server", dest="server", metavar="SERVER",
default="localhost", help="defines the server to use")
parser.add_option("-q", "--quiet", action="store_false", dest="verbose",
default=True, help="don't print status messages to stdout")
parser.add_option("-d", "--debug", dest="debug", default=False,
action="store_true", help="enable debugging outputs")
parser.add_option("-o", "--output", action="store", type="string",
dest="output", default="foobar", metavar="FILE",
help=("define CSV report filename. (default: "
"errata-snapshot-report-RHNhostname-Ymd.csv)")
)
parser.add_option("-f", "--field", action="append", type="choice",
dest="fields", choices=POSSIBLE_FIELDS, metavar="FIELDS",
help="defines which fields should be integrated in the report")
parser.add_option("-p", "--include-patches", action="store_true",
default=False, dest="includePatches",
help=("defines whether package updates that are not part of an "
"erratum shall be included")
)
parser.add_option("-r", "--reconnect-threshold", action="store",
type="int", default=5, dest="reconnectThreshold", metavar="THRESHOLD",
help=("defines after how many host scans a re-login should be done "
"(XMLRPC API timeout workaround)")
)
parser = OptionParser(description=desc, version="%prog version 0.2")

#-a / --authfile
parser.add_option("-a", "--authfile", dest="authfile", metavar="FILE", default="", help="defines an auth file to use instead of shell variables")
#-s / --server
parser.add_option("-s", "--server", dest="server", metavar="SERVER", default="localhost", help="defines the server to use")
#-q / --quiet
parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout")
#-d / --debug
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")
#-o / --output
parser.add_option("-o", "--output", action="store", type="string", dest="output", default="foobar", metavar="FILE", help=("define CSV report filename. (default: " "errata-snapshot-report-RHNhostname-Ymd.csv)"))
#-f / --field
parser.add_option("-f", "--field", action="append", type="choice", dest="fields", choices=POSSIBLE_FIELDS, metavar="FIELDS", help="defines which fields should be integrated in the report")
#-p / --include-patches
parser.add_option("-p", "--include-patches", action="store_true", default=False, dest="includePatches", help=("defines whether package updates that are not part of an erratum shall be included"))
#-r / --reconnect-threshold
parser.add_option("-r", "--reconnect-threshold", action="store", type="int", default=5, dest="reconnectThreshold", metavar="THRESHOLD", help=("defines after how many host scans a re-login should be done (XMLRPC API timeout workaround)"))

(options, args) = parser.parse_args(args)

Expand Down

0 comments on commit ef7fa39

Please sign in to comment.