def __init__(self, config): """ Initialization is responsible for fetching service instance objects for each vCenter instance pyvmomi has some security checks enabled by default from python 2.7.9 onward to connect to vCenter. """ # Holds all the VMs' instanceuuid that are discovered for each of the vCenter. Going ahead it would hold all the # other managed objects of vCenter that would be monitored. self.mors = {} # Now mars is act as <key,value>. here key is instance UUID and Values is morf Id self.params = config global metrics global counters metrics = util.parse_metrics() counters = util.parse_counters() self.needed_metrics = {} self.configured_metrics = {} self.refresh_rates = {} self.service_instance = "" for k, v in metrics.items(): self.configured_metrics.update({util.get_counter(k): v}) if sys.version_info > (2, 7, 9) and sys.version_info < (3, 0, 0): # https://www.python.org/dev/peps/pep-0476/ # Look for 'Opting out' section in this that talks about disabling the certificate verification # Following line helps to disable globally ssl._create_default_https_context = ssl._create_unverified_context # Disabling the security warning message, as the certificate verification is disabled urllib3.disable_warnings() try: service_instance = connect.SmartConnectNoSSL(host=self.params['host'], user=self.params['username'], pwd=self.params['password'], port=int(self.params['port'])) util.sendEvent("Plugin vmware", "Sucessfully connected to vCenter: [" + self.params['host'] + "]", "info") atexit.register(connect.Disconnect, service_instance) self.service_instance = service_instance self._cache_metrics_metadata(self.params['host']) except KeyError as ke: util.sendEvent("Plugin vmware: Key Error", "Improper param.json, key missing: [" + str(ke) + "]", "error") # sys.exit(-1) except ConnectionError as ce: util.sendEvent("Plugin vmware: Error connecting to vCenter", "Could not connect to the specified vCenter host: [" + str(ce) + "]", "critical") except Exception as se: util.sendEvent("Plugin vmware: Unknown Error", "[" + str(se) + "]", "critical") # sys.exit(-1) except vim.fault.InvalidLogin as il: util.sendEvent("Plugin vmware: Error logging into vCenter", "Could not login to the specified vCenter host: [" + str(il) + "]", "critical")
def start(self): self.vmware = VMWare(self.vcenter) self.discovery_thread = threading.Thread(target=self._discovery) self.discovery_thread.daemon = True self.discovery_thread.setName(self.vcenter['host'] + "_" + "Discovery") self.discovery_thread.start() while True: try: self._lock.acquire() self.vmware.collect() self._lock.release() time.sleep(float(self.vcenter.get("pollInterval", 1000) / 1000)) except Exception as se: self._lock.release() util.sendEvent("Plugin vmware: Unknown Error", "Unknown error occurred: [" + str(se) + "]", "critical") time.sleep(float(self.vcenter.get("pollInterval", 1000) / 1000)) self.terminate_thread(self.discovery_thread) # Killing old discovery thread util.sendEvent("Plugin vmware", "Trying to re-connect to vCenter: [" + self.vcenter['host'] + "]", "info") self.vmware = VMWare(self.vcenter) self.discovery_thread = threading.Thread(target=self._discovery) self.discovery_thread.daemon = True self.discovery_thread.setName(self.vcenter['host'] + "_" + "Discovery") self.discovery_thread.start()
def collect(self): """ This method is responsible to traverse through the mors[] and query metrics for each of the managed object that is discovered for all the vCenters. """ try: instance_key = self.params['host'] content = self.service_instance.RetrieveContent() search_index = self.service_instance.content.searchIndex polling_interval = self.params['pollInterval'] max_samples = self.params['maxSamples'] end_time = self.service_instance.CurrentTime() start_time = end_time - datetime.timedelta( seconds=polling_interval / 1000) except Exception as se: raise try: for uuid in self.mors.copy(): # checking key is exist or not vm = search_index.FindByUuid(None, uuid, True, True) if vm is not None: if uuid in self.needed_metrics: needed_metric_ids = self.needed_metrics[uuid] if uuid in self.refresh_rates: refresh_rate = self.refresh_rates[uuid] query = vim.PerformanceManager.QuerySpec( intervalId=refresh_rate, maxSample=max_samples, entity=vm, metricId=needed_metric_ids, startTime=start_time, endTime=end_time) result = content.perfManager.QueryPerf( querySpec=[query]) self._parse_result_and_publish( instance_key, vm.config.name, result, self.params['host'], self.params['app_id']) else: util.sendEvent( "Plugin vmware: Refresh Rate unavailable", "Refresh rate unavailable for a vm, ignoring", "warning") else: util.sendEvent( "Plugin vmware: Needed metrics unavailable", "Needed metrics unavailable for a vm, ignoring", "warning") except vmodl.MethodFault as error: # raise pass
def _discovery(self): # while True: try: # self._lock.acquire() # util.sendEvent("Plugin vmware: Discovery Cycle for " + self.vcenter['host'], "Running discovery cycle for " + self.vcenter['host'] + " started.", "info") self.vmware.discovery(self) # self._lock.release() # util.sendEvent("Plugin vmware: Discovery Cycle for " + self.vcenter['host'], "Running discovery cycle for " + self.vcenter['host'] + " completed.", "info") # time.sleep(self.vcenter.get("discoveryInterval", 10800000) / 1000) except Exception as se: util.sendEvent("Plugin vmware: Discovery error", "Unknown error occurred: [" + str(se) + "]", "error") if self._lock.locked: self._lock.release
def collect(self): """ This method is responsible to traverse through the mors[] and query metrics for each of the managed object that is discovered for all the vCenters. """ try: instance_key = self.params['host'] content = self.service_instance.RetrieveContent() search_index = self.service_instance.content.searchIndex polling_interval = self.params['pollInterval'] max_samples = self.params['maxSamples'] end_time = self.service_instance.CurrentTime() start_time = end_time - datetime.timedelta(seconds=polling_interval / 1000) except Exception as se: raise try: for uuid in self.mors.copy(): # checking key is exist or not vm = search_index.FindByUuid(None, uuid, True, True) if vm is not None: if uuid in self.needed_metrics: needed_metric_ids = self.needed_metrics[uuid] if uuid in self.refresh_rates: refresh_rate = self.refresh_rates[uuid] query = vim.PerformanceManager.QuerySpec(intervalId=refresh_rate, maxSample=max_samples, entity=vm, metricId=needed_metric_ids, startTime=start_time, endTime=end_time) result = content.perfManager.QueryPerf(querySpec=[query]) self._parse_result_and_publish(instance_key, vm.config.name, result, self.params['host'], self.params['app_id']) else: util.sendEvent("Plugin vmware: Refresh Rate unavailable", "Refresh rate unavailable for a vm, ignoring", "warning") else: util.sendEvent("Plugin vmware: Needed metrics unavailable", "Needed metrics unavailable for a vm, ignoring", "warning") except vmodl.MethodFault as error: # raise pass
def run(self): self.vmware = VMWare(self.vcenter) self.discovery_thread = threading.Thread(target=self._discovery) self.discovery_thread.daemon = True self.discovery_thread.setName(self.vcenter['host'] + "_" + "Discovery") self.discovery_thread.start() while True: try: self._lock.acquire() self.vmware.collect() self._lock.release() time.sleep(float(self.vcenter.get("pollInterval", 1000) / 1000)) except StandardError as se: util.sendEvent("Plugin vmware: Unknown Error", "Unknown error occurred: [" + str(se) + "]", "critical") if self._lock.locked: self._lock.release sys.exit(-1)
def make_property_collector(pc, from_node, props, self): """ :type pc: pyVmomi.VmomiSupport.vmodl.query.PropertyCollector :type from_node: pyVmomi.VmomiSupport.ManagedObject :type props: collections.Sequence :rtype: pyVmomi.VmomiSupport.vmodl.query.PropertyCollector.Filter """ # Make the filter spec filterSpec = vmodl.query.PropertyCollector.FilterSpec() # Make the object spec traversal = serviceutil.build_full_traversal() objSpec = vmodl.query.PropertyCollector.ObjectSpec(obj=from_node, selectSet=traversal) objSpecs = [objSpec] filterSpec.objectSet = objSpecs # Add the property specs propSet = [] for motype, proplist in props: propSpec = \ vmodl.query.PropertyCollector.PropertySpec(type=motype, all=False) propSpec.pathSet.extend(proplist) propSet.append(propSpec) filterSpec.propSet = propSet try: pcFilter = pc.CreateFilter(filterSpec, True) atexit.register(pcFilter.Destroy) return pcFilter except vmodl.MethodFault as e: if e._wsdlName == 'InvalidProperty': util.sendEvent("InvalidProperty", "InvalidProperty fault while creating: [" + str(e.name) + "]", "warning") else: util.sendEvent("Problem creating PropertyCollector", " filter : [" + str(e.faultMessage) + "]", "warning")
def run(self): self.vmware = VMWare(self.vcenter) self.discovery_thread = threading.Thread(target=self._discovery) self.discovery_thread.daemon = True self.discovery_thread.setName(self.vcenter['host'] + "_" + "Discovery") self.discovery_thread.start() while True: try: self._lock.acquire() self.vmware.collect() self._lock.release() time.sleep(float( self.vcenter.get("pollInterval", 1000) / 1000)) except StandardError as se: util.sendEvent("Plugin vmware: Unknown Error", "Unknown error occurred: [" + str(se) + "]", "critical") if self._lock.locked: self._lock.release sys.exit(-1)
def collect(self): """ This method is responsible to traverse through the mors[] and query metrics for each of the managed object that is discovered for all the vCenters. """ instance_key = self.params["host"] content = self.service_instance.RetrieveContent() search_index = self.service_instance.content.searchIndex polling_interval = self.params["pollInterval"] max_samples = self.params["maxSamples"] end_time = datetime.datetime.now() start_time = end_time - datetime.timedelta(seconds=polling_interval / 1000) try: if instance_key in self.mors: for uuid in self.mors[instance_key]: vm = search_index.FindByUuid(None, uuid, True, True) if vm is not None: if uuid in self.needed_metrics: needed_metric_ids = self.needed_metrics[uuid] if uuid in self.refresh_rates: refresh_rate = self.refresh_rates[uuid] query = vim.PerformanceManager.QuerySpec( intervalId=refresh_rate, maxSample=max_samples, entity=vm, metricId=needed_metric_ids, startTime=start_time, endTime=end_time, ) result = content.perfManager.QueryPerf(querySpec=[query]) self._parse_result_and_publish( instance_key, vm.config.name, result, self.params["host"], self.params["appId"] ) else: util.sendEvent( "Plugin vmware: Refresh Rate unavailable", "Refresh rate unavailable for a vm, ignoring", "warning", ) else: util.sendEvent( "Plugin vmware: Needed metrics unavailable", "Needed metrics unavailable for a vm, ignoring", "warning", ) except vmodl.MethodFault as error: util.sendEvent("Error", str(error), "error")
def waitForUpdate(self, discoverySelfInstance): try: si = SmartConnect(host=self.params['host'], user=self.params['username'], pwd=self.params['password'], port=int(self.params['port'])) if not si: util.sendEvent("Could not connect to the specified host", " : [" + self.params['password'] + self.params['username'] + "]", " critical ") raise atexit.register(Disconnect, si) propertiesSpecification = []; propertiesSpecification = ['VirtualMachine:name,summary.config.instanceUuid'] propspec = parse_propspec(propertiesSpecification) monitor_property_changes(si, propspec, self, discoverySelfInstance, 1) except vmodl.MethodFault as e: util.sendEvent("Plugin vmware:", " Caught vmodl fault : [" + str(e) + "]", "warning") except Exception as e: util.sendEvent("Plugin vmware:", " Caught exception : [" + str(e) + "]", "warning")
self._lock.acquire() self.vmware.collect() self._lock.release() time.sleep(float(self.vcenter.get("pollInterval", 1000) / 1000)) except Exception as se: self._lock.release() util.sendEvent("Plugin vmware: Unknown Error", "Unknown error occurred: [" + str(se) + "]", "critical") time.sleep(float(self.vcenter.get("pollInterval", 1000) / 1000)) self.terminate_thread(self.discovery_thread) # Killing old discovery thread util.sendEvent("Plugin vmware", "Trying to re-connect to vCenter: [" + self.vcenter['host'] + "]", "info") self.vmware = VMWare(self.vcenter) self.discovery_thread = threading.Thread(target=self._discovery) self.discovery_thread.daemon = True self.discovery_thread.setName(self.vcenter['host'] + "_" + "Discovery") self.discovery_thread.start() if __name__ == "__main__": params = util.parse_params() util.sendEvent("Plugin started", "Started vmware plugin", "info") for vcenter in params['items']: thread = CollectionThread(vcenter) thread.daemon = True thread.setName(vcenter['host']) thread.start() while True: time.sleep(60)
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # This script is called after the plugin files have been copied to the target host file system from bootstrap import Bootstrap from modules import util import sys def isPythonVesrsionSupported(): currentPythonVersion = sys.version_info pluginSupportedPythonVersions = ['2.7.5','2.7.6','2.7.7','2.7.8','2.7.9','2.7.10','2.7.11'] isPythonVersionSupported = False currentPythonVersions = str(currentPythonVersion[0]) + "." +str(currentPythonVersion[1]) +"."+str(currentPythonVersion[2]) for version in pluginSupportedPythonVersions: if currentPythonVersions == version: isPythonVersionSupported = True break return isPythonVersionSupported if __name__ == "__main__": if (isPythonVesrsionSupported()): bootstrap = Bootstrap() bootstrap.setup() else: currentPythonVersion = sys.version_info util.sendEvent("Plugin vmware:", "Python version not supported: [" + str(currentPythonVersion[0]) + "." +str(currentPythonVersion[1]) +"."+str(currentPythonVersion[2])+"]", "error") sys.exit(-1)
import sys def isPythonVesrsionSupported(): currentPythonVersion = sys.version_info pluginSupportedPythonVersions = [ '2.7.5', '2.7.6', '2.7.7', '2.7.8', '2.7.9', '2.7.10', '2.7.11', '3.3.5', '3.4.2' ] isPythonVersionSupported = False currentPythonVersions = str(currentPythonVersion[0]) + "." + str( currentPythonVersion[1]) + "." + str(currentPythonVersion[2]) for version in pluginSupportedPythonVersions: if currentPythonVersions == version: isPythonVersionSupported = True break return isPythonVersionSupported if __name__ == "__main__": if (isPythonVesrsionSupported()): bootstrap = Bootstrap() bootstrap.setup() else: currentPythonVersion = sys.version_info util.sendEvent( "Plugin vmware:", "Python version not supported: [" + str(currentPythonVersion[0]) + "." + str(currentPythonVersion[1]) + "." + str(currentPythonVersion[2]) + "]", "error") sys.exit(-1)
def __init__(self, config): """ Initialization is responsible for fetching service instance objects for each vCenter instance pyvmomi has some security checks enabled by default from python 2.7.9 onward to connect to vCenter. """ # Holds all the VMs' instanceuuid that are discovered for each of the vCenter. Going ahead it would hold all the # other managed objects of vCenter that would be monitored. self.mors = { } # Now mars is act as <key,value>. here key is instance UUID and Values is morf Id self.params = config global metrics global counters metrics = util.parse_metrics() counters = util.parse_counters() self.needed_metrics = {} self.configured_metrics = {} self.refresh_rates = {} self.service_instance = "" for k, v in metrics.items(): self.configured_metrics.update({util.get_counter(k): v}) if sys.version_info > (2, 7, 9) and sys.version_info < (3, 0, 0): # https://www.python.org/dev/peps/pep-0476/ # Look for 'Opting out' section in this that talks about disabling the certificate verification # Following line helps to disable globally ssl._create_default_https_context = ssl._create_unverified_context # Disabling the security warning message, as the certificate verification is disabled urllib3.disable_warnings() try: service_instance = connect.SmartConnectNoSSL( host=self.params['host'], user=self.params['username'], pwd=self.params['password'], port=int(self.params['port'])) util.sendEvent( "Plugin vmware", "Sucessfully connected to vCenter: [" + self.params['host'] + "]", "info") atexit.register(connect.Disconnect, service_instance) self.service_instance = service_instance self._cache_metrics_metadata(self.params['host']) except KeyError as ke: util.sendEvent( "Plugin vmware: Key Error", "Improper param.json, key missing: [" + str(ke) + "]", "error") # sys.exit(-1) except ConnectionError as ce: util.sendEvent( "Plugin vmware: Error connecting to vCenter", "Could not connect to the specified vCenter host: [" + str(ce) + "]", "critical") except Exception as se: util.sendEvent("Plugin vmware: Unknown Error", "[" + str(se) + "]", "critical") # sys.exit(-1) except vim.fault.InvalidLogin as il: util.sendEvent( "Plugin vmware: Error logging into vCenter", "Could not login to the specified vCenter host: [" + str(il) + "]", "critical")
self.vcenter['host'] + " started.", "info") self.vmware.discovery() self._lock.release() util.sendEvent( "Plugin vmware: Discovery Cycle for " + self.vcenter['host'], "Running discovery cycle for " + self.vcenter['host'] + " completed.", "info") time.sleep( self.vcenter.get("discoveryInterval", 10800000) / 1000) except StandardError as se: util.sendEvent("Unknown Error", "Unknown error occurred: [" + str(se) + "]", "error") if self._lock.locked: self._lock.release sys.exit(-1) if __name__ == "__main__": params = util.parse_params() util.sendEvent("Plugin started", "Started vmware plugin", "info") for vcenter in params['items']: thread = CollectionThread(vcenter) thread.daemon = True thread.setName(vcenter['host']) thread.start() while True: time.sleep(60)