-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_nagios_agent.py
executable file
·133 lines (112 loc) · 3.89 KB
/
install_nagios_agent.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2013, Pall Sigurdsson <palli@opensource.is>
#
# This script 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 script 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/>.
# This script will is a wrapper around "okconfig install" and will
# install NRPE on a remote server and minimal amount of plugins
# to satisfy the a typical "okconfig" client.
# This script was made for Jan Stembera.
import okconfig
import simplejson as json
import sys
import paramiko
# map x0* arguments to something meaningful
argmap = {}
argmap['x01'] = 'username'
argmap['x02'] = 'password'
argmap['x03'] = 'domain'
argmap['x04'] = 'host'
argmap['x06'] = 'platform'
def main():
""" Main function of the program """
arguments = parse_arguments()
remote_host = arguments.get('host') or usage('remote host was not provided')
username = arguments.get('username') or usage('username was not provided')
password = arguments.get('password') or usage('password was not provided')
domain = arguments.get('domain', None)
platform = arguments.get('platform', None)
install_method = None
if 'linux' in platform:
install_method = 'ssh'
elif 'windows' in platform:
install_method = 'winexe'
status, stdout, stderr = okconfig.install_okagent(
remote_host=remote_host,
username=username,
password=password,
domain=domain,
install_method=install_method,
)
if status == 0:
success(stdout)
else:
error(stdout)
def error(message, json_data=None):
""" Prints out a json error to the screen and exits the program """
print_json(False, message, json_data)
sys.exit(1)
def success(message, json_data=None):
""" Prints out a json message and exits the program """
print_json(True, message, json_data)
sys.exit(0)
def print_json(success, message, json_data=None):
""" converts success and message to json dictionary and prints to stdout """
if not json_data:
json_data = {}
json_data['success'] = success
json_data['message'] = message
print json.dumps(json_data, indent=4)
def parse_arguments(*args):
""" Parse command line arguments
Arguments:
*args : a list of arguments, usually sys.argv
"""
result = {} # Dictionary containing the arguments
if not args:
args = sys.argv[1:]
if '--help' in args:
usage()
for arg in args:
if not '=' in arg:
raise ValueError("Invalid argument: %s" % arg)
key, value = arg.split('=', 1)
result[key] = value
if key in argmap:
key = argmap[key]
result[key] = value
return result
def usage(message=None):
""" Print usage of the program and exit """
if message is not None:
print message
print ""
print "Usage: %s x01=USERNAME x02=PASSWORD x03=? x04=REMOTE_HOST" % sys.argv[0]
print """
x01: Username used to log into remote server
x02: Password used to log into remote server
x03: Domain name
x04: Hostname or IP Address of remote host to deploy on
x06: Platform (either windows or linux)
"""
sys.exit(0)
if __name__ == '__main__':
try:
main()
except paramiko.AuthenticationException:
error("Authentication failed")
except SystemExit:
pass
except Exception, e:
error(str(e), json_data={'error_type': type(e)})