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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | #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() |
监控数据参见:
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | (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) [] } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步