2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
6 # Hartmut Goebel <h.goebel@goebel-consult.de>
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
28 from subprocess
import Popen
, PIPE
30 # Try to load json (2.5 and higer) or simplejson if failed (python2.4)
34 # For old Python version, load
35 # simple json (it can be hard json?! It's 2 functions guy!)
37 import simplejson
as json
39 sys
.exit("Error : you need the json or simplejson module for this script")
44 # Split and clean the rules from a string to a list
45 def _split_rules(rules
):
46 return [r
.strip() for r
in rules
.split('|')]
48 # Apply all rules on the objects names
49 def _apply_rules(name
, rules
):
51 name
= name
.split('.', 1)[0]
56 # Get all vmware hosts from a VCenter and return the list
57 def get_vmware_hosts(check_esx_path
, vcenter
, user
, password
):
58 list_host_cmd
= [check_esx_path
, '-D', vcenter
, '-u', user
, '-p', password
,
59 '-l', 'runtime', '-s', 'listhost']
61 output
= Popen(list_host_cmd
, stdout
=PIPE
).communicate()
63 parts
= output
[0].split(':')
64 hsts_raw
= parts
[1].split('|')[0]
65 hsts_raw_lst
= hsts_raw
.split(',')
68 for hst_raw
in hsts_raw_lst
:
69 hst_raw
= hst_raw
.strip()
70 # look as server4.mydomain(UP)
71 elts
= hst_raw
.split('(')
78 # For a specific host, ask all VM on it to the VCenter
79 def get_vm_of_host(check_esx_path
, vcenter
, host
, user
, password
):
80 print "Listing host", host
81 list_vm_cmd
= [check_esx_path
, '-D', vcenter
, '-H', host
,
82 '-u', user
, '-p', password
,
83 '-l', 'runtime', '-s', 'list']
84 output
= Popen(list_vm_cmd
, stdout
=PIPE
).communicate()
85 parts
= output
[0].split(':')
86 # Maybe we got a 'CRITICAL - There are no VMs.' message,
87 # if so, we bypass this host
91 vms_raw
= parts
[1].split('|')[0]
92 vms_raw_lst
= vms_raw
.split(',')
95 for vm_raw
in vms_raw_lst
:
96 vm_raw
= vm_raw
.strip()
98 elts
= vm_raw
.split('(')
104 # Create all tuples of the links for the hosts
105 def create_all_links(res
, rules
):
109 # First we apply rules on the names
110 host_name
= _apply_rules(host
, rules
)
111 vm_name
= _apply_rules(vm
, rules
)
112 v
= (('host', host_name
),('host', vm_name
))
117 def write_output(r
, path
):
119 f
= open(path
+'.tmp', 'wb')
123 shutil
.move(path
+'.tmp', path
)
124 print "File %s wrote" % path
126 sys
.exit("Error writing the file %s : %s" % (path
, exp
))
129 def main(check_esx_path
, vcenter
, user
, password
, output
, rules
):
130 rules
= _split_rules(rules
)
132 hosts
= get_vmware_hosts(check_esx_path
, vcenter
, user
, password
)
135 lst
= get_vm_of_host(check_esx_path
, vcenter
, host
, user
, password
)
139 r
= create_all_links(res
, rules
)
140 print "Created %d links" % len(r
)
142 write_output(r
, output
)
147 if __name__
== "__main__":
149 parser
= optparse
.OptionParser(
150 version
="Shinken VMware links dumping script version %s" % VERSION
)
151 parser
.add_option("-o", "--output",
152 help="Path of the generated mapping file.")
153 parser
.add_option("-x", "--esx3-path", dest
='check_esx_path',
154 default
='/usr/local/nagios/libexec/check_esx3.pl',
155 help="Full path of the check_esx3.pl script (default: %default)")
156 parser
.add_option("-V", "--vcenter", '--Vcenter',
157 help="tThe IP/DNS address of your Vcenter host.")
158 parser
.add_option("-u", "--user",
159 help="User name to connect to this Vcenter")
160 parser
.add_option("-p", "--password",
161 help="The password of this user")
162 parser
.add_option('-r', '--rules', default
='',
163 help="Rules of name transformation. Valid names are: "
164 "`lower`: to lower names, "
165 "`nofqdn`: keep only the first name (server.mydomain.com -> server)."
166 "You can use several rules like `lower|nofqdn`")
168 opts
, args
= parser
.parse_args()
170 parser
.error("does not take any positional arguments")
172 if opts
.vcenter
is None:
173 parser
.error("missing -V or --Vcenter option for the vcenter IP/DNS address")
174 if opts
.user
is None:
175 parser
.error("missing -u or --user option for the vcenter username")
176 if opts
.password
is None:
178 parser
.error("missing -p or --password option for the vcenter password")
179 if not os
.path
.exists(opts
.check_esx_path
):
180 parser
.error("the path %s for the check_esx3.pl script is wrong, missing file" % opts
.check_esx_path
)
181 if opts
.output
is None:
182 parser
.error("missing -o or --output option for the output mapping file")
184 main(**opts
.__dict
__)