zoukankan      html  css  js  c++  java
  • python监控虚拟机

    #coding=utf8
    import atexit
    from pyVmomi import vim, vmodl
    from pyVim.connect import SmartConnect, Disconnect
    import sys
    import copy
    import requests
    import time
    import json
    import pymysql
    import ssl
    from datetime import timedelta
    import datetime
    db = pymysql.connect('', port=3306, user='root', passwd='', db='')
    now = int(time.time())
    
    def VmInfo(vm, content, vchtime, interval, perf_dict, name,ip):
        try:
            statInt = interval / 20
            summary = vm.summary
            stats = summary.quickStats
            memoryUsage = stats.guestMemoryUsage * 1024 * 1024#
            memoryCapacity = summary.runtime.maxMemoryUsage * 1024 * 1024
            maxMemoryPercentage =(float(memoryUsage) / float(memoryCapacity)) * 100
            if not ip:
                ip=vm_raw_infos.get(name)
            add_data("vm.memory.maxPercent", float(maxMemoryPercentage), "GAUGE", name,ip)
        except Exception as error:
            if not ip:
                ip=vm_raw_infos.get(name)
            add_data("vm.memory.maxPercent", 'no data', "GAUGE", name, ip)
    
    
    def add_data(metric, value, conterType, name,ip):
        global now,payload
        if isinstance(value,float):
            has_data=True
        else:
            has_data=False
        data = {"metric": metric, "timestamp": now, "step": interval, "value": value,
                "counterType": conterType, "name": name,'ip':ip,'has_data':has_data}
        # payload.append(copy.copy(data))
        payload.update({ip:data})
    
    
    def run(host, user, pwd, port, interval):
        global vm_ip_infos,vm_raw_infos,vms,vm_name_infos
        try:
            context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            si = SmartConnect(host=host, user=user, pwd=pwd, port=port,sslContext=context)
            atexit.register(Disconnect, si)
            content = si.RetrieveContent()
            vchtime = si.CurrentTime()
    
            perf_dict = {}
            perfList = content.perfManager.perfCounter
            for counter in perfList:
                counter_full = "{}.{}.{}".format(counter.groupInfo.key, counter.nameInfo.key, counter.rollupType)
                perf_dict[counter_full] = counter.key
    
            if True: #config.vm_enable == True:
                obj = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
                for vm in obj.view:
                    summary = vm.summary
                    boot_time = summary.runtime.bootTime
                    ip = summary.guest.ipAddress
                    if vm.name and vm.name in vm_name_infos:
                        VmInfo(vm, content, vchtime, interval, perf_dict, vm.name,ip)
                    elif vm.name in vms['stop']:
                        add_data("vm.memory.freePercent", 'PowerDown', "GAUGE", vm.name, ip)
                    elif vm.name in vms['other']:
                        add_data("vm.memory.freePercent", 'vmTools Error', "GAUGE", vm.name, vm_raw_infos.get(vm.name))
                    elif vm.name in vms['start']:
                        add_data("vm.memory.freePercent", '--', "GAUGE", vm.name, ip)
        except vmodl.MethodFault as error:
            print "Caught vmodl fault : " + error.msg
            return False, error.msg
        return True, "ok"
    
    def hello_vcenter(vchost, username, password, port):
        try:
            context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            si = SmartConnect(
                host=vchost,
                user=username,
                pwd=password,
                port=port,sslContext=context)
    
            atexit.register(Disconnect, si)
            return True, "ok"
        except vmodl.MethodFault as error:
            return False, error.msg
        except Exception as e:
            return False, str(e)
    
    
    def BuildQuery(content, vchtime, counterId, instance, entity, interval):
        perfManager = content.perfManager
        metricId = vim.PerformanceManager.MetricId(counterId=counterId, instance=instance)
        startTime = vchtime - timedelta(seconds=(interval + 60))
        endTime = vchtime - timedelta(seconds=60)
        query = vim.PerformanceManager.QuerySpec(intervalId=20, entity=entity, metricId=[metricId], startTime=startTime,
                                                 endTime=endTime)
        perfResults = perfManager.QueryPerf(querySpec=[query])
        if perfResults:
            return perfResults
        else:
            return False
    
    def get_status():
        from pysphere import VIServer
        global vm_raw_infos,vm_name_infos
        vmnames=[]
        ssl._create_default_https_context = ssl._create_unverified_context
        server = VIServer()
        server.connect('', '', '')
        vm_infos={'stop':[],'start':[],'other':[]}
        for i in vm_name_infos:
            try:
                pvm = server.get_vm_by_name(i)
                status=pvm.get_status()
                if status=='POWERED OFF':
                    vm_infos['stop'].append(i)
                elif status=='POWERED ON':
                    vm_infos['start'].append(i)
                else:
                    vm_infos['other'].append(i)
            except Exception as e:
                vm_infos['other'].append(i)
        return vm_infos
    
    def get_datas():
        vm_infos = {}
        global db
        cursor = db.cursor()
        sql = "select * from vminfo where status='used' and owner not in('unknown','',' ' );"
        cursor.execute(sql)
        datas = cursor.fetchall()
        for i in datas:
            vm_infos.update({i[2]:i[0]})
        return vm_infos
    
    
    def drop_data():
        global db
        cursor = db.cursor()
        sql = "delete from vm_memory where date(insert_time) < date_sub(curdate(), INTERVAL 60 DAY)"
        print sql
        cursor.execute(sql)
        db.commit()
    
        cursor = db.cursor()
        sql = "delete from vm_memory_fail where date(insert_time) < date_sub(curdate(), INTERVAL 60 DAY)"
        print sql
        cursor.execute(sql)
        db.commit()
    
    
    if __name__ == "__main__":
        host = ''
        user = ''
        pwd =  ''
        port = 443
        interval=60
    
        payload = {}
    
        #获取当前已申请的虚拟机记录清单
        vm_raw_infos=get_datas()
        vm_ip_infos=vm_raw_infos.values()
        vm_name_infos=vm_raw_infos.keys()
    
        # 测试vsphere连接
        success, msg = hello_vcenter(host, user, pwd, port)
        if success == False:
            print 'vcenter登录失败'
            sys.exit(1)
    
        #获取虚拟机状态
        vms=get_status()
        #获取监控数据
        run(host, user, pwd, port, interval)
        # print json.dumps(payload,indent=4)
        result={}
        error_result=[]
        vm_ips=payload.keys()
        timeArray = time.localtime(now)
        insert_time = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    
        # 删除数据库里超过两个月的记录
        drop_data()
    
        #获取内存信息并插入数据库
        insert_data=[]
        for name,ip in vm_raw_infos.items():
            if ip in vm_ips and not isinstance(payload[ip]['value'],float):
                # error_result.update({ip:  payload[ip]['value']})
                error_result.append((name, ip, insert_time,payload[ip]['value']))
            elif  ip in vm_ips and isinstance(payload[ip]['value'],float):
                insert_data.append((name, ip, insert_time,payload[ip]['value']))
            else:
                error_result.append((name, ip, insert_time, 'vmTools Error Or Powerdown'))
                # error_result.update({ip: 'vmTools Error Or Powerdown'})
    
        with db.cursor() as cursor:
            sql = "insert into vm_memory(name,ip,insert_time,value) values(%s,%s,%s,%s)"
            cursor.executemany(sql, insert_data)
            db.commit()
    
        with db.cursor() as cursor:
            sql = "insert into vm_memory_fail(name,ip,insert_time,value) values(%s,%s,%s,%s)"
            cursor.executemany(sql, error_result)
            db.commit()
    
        #生成虚拟机状态表
        vm_result=[]
        for name,ip in vm_raw_infos.items():
            if name in vms['start']:
                vm_result.append((ip,insert_time,'开机'))
            if name in vms['stop']:
                vm_result.append((ip, insert_time, '停机'))
            if name in vms['other']:
                vm_result.append((ip, insert_time, '其他'))
    
    
        with db.cursor() as cursor:
            sql = "truncate table vm_status"
            cursor.execute(sql)
            db.commit()
    
            sql = "insert into vm_status(ip,insert_time,status) values(%s,%s,%s)"
            cursor.executemany(sql, vm_result)
            db.commit()
    

      监控数据参见:

    (vim.vm.Summary) {
       dynamicType = <unset>,
       dynamicProperty = (vmodl.DynamicProperty) [],
       vm = 'vim.VirtualMachine:vm-1181',
       runtime = (vim.vm.RuntimeInfo) {
          dynamicType = <unset>,
          dynamicProperty = (vmodl.DynamicProperty) [],
          device = (vim.vm.DeviceRuntimeInfo) [
             (vim.vm.DeviceRuntimeInfo) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                runtimeState = (vim.vm.DeviceRuntimeInfo.VirtualEthernetCardRuntimeState) {
                   dynamicType = <unset>,
                   dynamicProperty = (vmodl.DynamicProperty) [],
                   vmDirectPathGen2Active = false,
                   vmDirectPathGen2InactiveReasonVm = (str) [],
                   vmDirectPathGen2InactiveReasonOther = (str) [
                      'vmNptIncompatibleNetwork'
                   ],
                   vmDirectPathGen2InactiveReasonExtended = <unset>,
                   reservationStatus = <unset>
                },
                key = 4000
             }
          ],
          host = 'vim.HostSystem:host-329',
          connectionState = 'connected',
          powerState = 'poweredOn',
          faultToleranceState = 'notConfigured',
          dasVmProtection = <unset>,
          toolsInstallerMounted = false,
          suspendTime = <unset>,
          bootTime = 2019-07-25T06:32:58.834961Z,
          suspendInterval = 0L,
          question = <unset>,
          memoryOverhead = 250761216L,
          maxCpuUsage = 9596,
          maxMemoryUsage = 32768,
          numMksConnections = 0,
          recordReplayState = 'inactive',
          cleanPowerOff = <unset>,
          needSecondaryReason = <unset>,
          onlineStandby = false,
          minRequiredEVCModeKey = <unset>,
          consolidationNeeded = false,
          offlineFeatureRequirement = (vim.vm.FeatureRequirement) [
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.lm',
                featureName = 'cpuid.lm',
                value = 'Bool:Min:1'
             }
          ],
          featureRequirement = (vim.vm.FeatureRequirement) [
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.AES',
                featureName = 'cpuid.AES',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.AVX',
                featureName = 'cpuid.AVX',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.CMPXCHG16B',
                featureName = 'cpuid.CMPXCHG16B',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.DS',
                featureName = 'cpuid.DS',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.FMA',
                featureName = 'cpuid.FMA',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.Intel',
                featureName = 'cpuid.Intel',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.LAHF64',
                featureName = 'cpuid.LAHF64',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.LM',
                featureName = 'cpuid.LM',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.MOVBE',
                featureName = 'cpuid.MOVBE',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.NX',
                featureName = 'cpuid.NX',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.PCLMULQDQ',
                featureName = 'cpuid.PCLMULQDQ',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.POPCNT',
                featureName = 'cpuid.POPCNT',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.RDTSCP',
                featureName = 'cpuid.RDTSCP',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.SS',
                featureName = 'cpuid.SS',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.SSE3',
                featureName = 'cpuid.SSE3',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.SSE41',
                featureName = 'cpuid.SSE41',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.SSE42',
                featureName = 'cpuid.SSE42',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.SSSE3',
                featureName = 'cpuid.SSSE3',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.XCR0_MASTER_SSE',
                featureName = 'cpuid.XCR0_MASTER_SSE',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.XCR0_MASTER_YMM_H',
                featureName = 'cpuid.XCR0_MASTER_YMM_H',
                value = 'Bool:Min:1'
             },
             (vim.vm.FeatureRequirement) {
                dynamicType = <unset>,
                dynamicProperty = (vmodl.DynamicProperty) [],
                key = 'cpuid.XSAVE',
                featureName = 'cpuid.XSAVE',
                value = 'Bool:Min:1'
             }
          ],
          featureMask = (vim.host.FeatureMask) [],
          vFlashCacheAllocation = <unset>,
          paused = <unset>,
          snapshotInBackground = <unset>,
          quiescedForkParent = <unset>
       },
       guest = (vim.vm.Summary.GuestSummary) {
          dynamicType = <unset>,
          dynamicProperty = (vmodl.DynamicProperty) [],
          guestId = 'centos64Guest',
          guestFullName = u'CentOS 4/5/6 (64 u4f4d)',
          toolsStatus = 'toolsOk',
          toolsVersionStatus = 'guestToolsCurrent',
          toolsVersionStatus2 = 'guestToolsCurrent',
          toolsRunningStatus = 'guestToolsRunning',
          hostName = 'localhost.localdomain',
          ipAddress = '10.1.61.123'
       },
       config = (vim.vm.Summary.ConfigSummary) {
          dynamicType = <unset>,
          dynamicProperty = (vmodl.DynamicProperty) [],
          name = '61.123-platform',
          template = false,
          vmPathName = '[Innovative_SAAS1] 61.123-platform/61.123-platform.vmx',
          memorySizeMB = 32768,
          cpuReservation = 0,
          memoryReservation = 0,
          numCpu = 4,
          numEthernetCards = 1,
          numVirtualDisks = 1,
          uuid = '4239f236-e960-1366-f69e-cfec7698fe71',
          instanceUuid = '50394433-de7a-7b91-e828-3f9169f05029',
          guestId = 'centos64Guest',
          guestFullName = u'CentOS 4/5/6 (64 u4f4d)',
          annotation = '',
          product = <unset>,
          installBootRequired = false,
          ftInfo = <unset>,
          managedBy = <unset>
       },
       storage = (vim.vm.Summary.StorageSummary) {
          dynamicType = <unset>,
          dynamicProperty = (vmodl.DynamicProperty) [],
          committed = 63243843022L,
          uncommitted = 14165213184L,
          unshared = 63243843022L,
          timestamp = 2019-08-23T02:00:26.8525Z
       },
       quickStats = (vim.vm.Summary.QuickStats) {
          dynamicType = <unset>,
          dynamicProperty = (vmodl.DynamicProperty) [],
          overallCpuUsage = 1919,
          overallCpuDemand = 2878,
          guestMemoryUsage = 1638,
          hostMemoryUsage = 32017,
          guestHeartbeatStatus = 'green',
          distributedCpuEntitlement = 2878,
          distributedMemoryEntitlement = 9759,
          staticCpuEntitlement = 202,
          staticMemoryEntitlement = 12617,
          privateMemory = 31630,
          sharedMemory = 1008,
          swappedMemory = 0,
          balloonedMemory = 0,
          consumedOverheadMemory = 187,
          ftLogBandwidth = -1,
          ftSecondaryLatency = -1,
          ftLatencyStatus = 'gray',
          compressedMemory = 0L,
          uptimeSeconds = 2493235,
          ssdSwappedMemory = 0L
       },
       overallStatus = 'green',
       customValue = (vim.CustomFieldsManager.Value) []
    }
    

      

  • 相关阅读:
    网络编程之即时通信程序(聊天室)(一)通信流程简介及通信协议定制
    C#常用加密方法解析
    ASP.NET常用数据绑定控件优劣总结
    使用XPO开发时可以参考使用的架构
    渠道会上的体会
    如何利用第三方SDK开发MSN机器人以及实现语音视频?
    对 XPO 的一些问题的解答
    c++ 参数传递 之引用形参 GIS
    指针与引用的区别 GIS
    c++ const 修饰数组 GIS
  • 原文地址:https://www.cnblogs.com/slqt/p/11381886.html
Copyright © 2011-2022 走看看