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
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
27 # Try to load the json (2.5 and higer) or
28 # the simplejson if failed (python2.4)
32 # For old Python version, load
33 # simple json (it can be hard json?! It's 2 functions guy!)
35 import simplejson
as json
37 print "Error : you need the json or simplejson module for this script"
39 from subprocess
import Popen
, PIPE
42 # Split and clean the rules from a string to a list
43 def split_rules(rules
):
47 new_rules
.append(e
.strip())
51 # Apply all rules on the objects names
52 def _apply_rules(name
, rules
):
60 # Get all vmware hosts from a VCenter and return the list
61 def get_vmware_hosts(check_esx_path
, vcenter
, user
, password
):
62 list_host_cmd_s
= '%s -D %s -u %s -p %s -l runtime -s listhost' % (check_esx_path
, vcenter
, user
, password
)
63 list_host_cmd
= shlex
.split(list_host_cmd_s
)
67 output
= Popen(list_host_cmd
, stdout
=PIPE
).communicate()
69 parts
= output
[0].split(':')
70 hsts_raw
= parts
[1].split('|')[0]
71 hsts_raw_lst
= hsts_raw
.split(',')
73 for hst_raw
in hsts_raw_lst
:
74 hst_raw
= hst_raw
.strip()
75 # look as server4.mydomain(UP)
76 elts
= hst_raw
.split('(')
83 # For a specific host, ask all VM on it to the VCenter
84 def get_vm_of_host(check_esx_path
, vcenter
, h
, user
, password
):
86 print "Listing host", h
87 list_vm_cmd_s
= '%s -D %s -H %s -u %s -p %s -l runtime -s list' % (check_esx_path
, vcenter
, h
, user
, password
)
88 list_vm_cmd
= shlex
.split(list_vm_cmd_s
)
89 output
= Popen(list_vm_cmd
, stdout
=PIPE
).communicate()
90 parts
= output
[0].split(':')
91 # Maybe we got a 'CRITICAL - There are no VMs.' message,
92 # if so, we bypass this host
96 vms_raw
= parts
[1].split('|')[0]
97 vms_raw_lst
= vms_raw
.split(',')
99 for vm_raw
in vms_raw_lst
:
100 vm_raw
= vm_raw
.strip()
102 elts
= vm_raw
.split('(')
108 # Create all tuples of the links for the hosts
109 def create_all_links(res
, rules
):
113 # First we apply rules on the names
114 host_name
= _apply_rules(host
, rules
)
115 vm_name
= _apply_rules(vm
, rules
)
116 v
= (('host', host_name
),('host', vm_name
))
121 def write_output(r
, path
):
123 f
= open(path
+'.tmp', 'wb')
127 shutil
.move(path
+'.tmp', path
)
128 print "File %s wrote" % path
130 print "Error writing the file %s : %s" % (path
, exp
)
134 def main(check_esx_path
, vcenter
, user
, password
, output
, rules
):
135 rules
= split_rules(rules
)
137 hosts
= get_vmware_hosts(check_esx_path
, vcenter
, user
, password
)
140 lst
= get_vm_of_host(check_esx_path
, vcenter
, h
, user
, password
)
144 r
= create_all_links(res
, rules
)
145 print "Created %d links" % len(r
)
147 write_output(r
, output
)
154 print "Shinken VMware links dumping script version %s from :" % VERSION
155 print " Gabes Jean, naparuba@gmail.com"
156 print " Gerhard Lausser, Gerhard.Lausser@consol.de"
157 print "Usage: %s -V vcenter-ip -u USER -p PASSWORD -o /tmp/vmware_link.json [--esx3-path /full/path/check_esx3.pl --rules RULES" % name
159 print " -V, --Vcenter"
160 print "\tThe IP/DNS address of your Vcenter host."
162 print "\tUser name to connect to this Vcenter"
163 print " -p, --password"
164 print "\tThe password of this user"
165 print " -o, --output"
166 print "\tPath of the generated mapping file."
167 print " -x, --esx3-path"
168 print "\tFull path of the check_esx3.pl script. By default /usr/local/nagios/libexec/check_esx3.pl"
170 print "\t Rules of name transformation:"
171 print "\t\t lower : to lower names"
172 print "\t\t nofqdn : keep only the first name (server.mydomain.com -> server)"
173 print "\t\t you can use several rules like 'lower|nofqdn'"
175 print "\tPrint detailed help screen"
178 print "\t %s -V vcenter.google.com -user MySuperUser -password secret --esx3-path /usr/local/nagios/libexec/check_esx3.pl --rules 'lower|nofqdn'" % name
181 def check_args(check_esx_path
, vcenter
, user
, password
, output
, rules
):
185 print "Error : missing -V or -Vcenter option for the vcenter IP/DNS address"
188 print "Error : missing -u or -user option for the vcenter username"
191 print "Error : missing -p or -password option for the vcenter password"
192 if not os
.path
.exists(check_esx_path
):
194 print "Error : the path %s for the check_esx3.pl script is wrong, missing file"
197 print "Error : missing -o or -output option for the output mapping file"
208 if __name__
== "__main__":
212 opts
, args
= getopt
.getopt(sys
.argv
[1:], "ho:x:V:u:p:r:", ["help", "output", "esx3-path", "Vcenter", "user", "password", "rules"])
213 except getopt
.GetoptError
, err
:
214 # print help information and exit:
215 print str(err
) # will print something like "option -a not recognized"
220 check_esx_path
= '/usr/local/nagios/libexec/check_esx3.pl'
227 if o
in ("-h", "--help"):
230 elif o
in ("-o", "--output"):
231 print "Got output", a
233 elif o
in ("-x", "--esx3-path"):
235 elif o
in ("-V", "--Vcenter"):
237 elif o
in ("-u", "--user"):
239 elif o
in ("-p", "--password"):
241 elif o
in ('-r', '--rules'):
244 print "Sorry, the option", o
, a
, "is unknown"
248 print "Got", check_esx_path
, vcenter
, user
, password
, output
, rules
249 check_args(check_esx_path
, vcenter
, user
, password
, output
, rules
)
250 main(check_esx_path
, vcenter
, user
, password
, output
, rules
)