Turn on the python.d collector of Datakit, enter the conf.d/pythond directory under the DataKit installation directory, copy pythond.conf.sample and rename it to pythond.conf. The example is as follows:
# {"version": "1.16.0", "desc": "do NOT edit this line"}[[inputs.pythond]]# Python input namename='some-python-inputs'# required# System environments to run Python#envs = ['LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH',]# Python path(recomment abstract Python path)cmd="/root/anaconda3/envs/py3.9/bin/python"# required. python3 is recommended.# Python scripts relative pathdirs=["vm"]
Note: Here cmd needs to be changed to your own Python environment address, which is used to execute Python scripts, and dirs is the directory where the scripts are stored.
Create a directory named after the "Python package name" under the datakit/python.d directory, and then create a Python script (.py) in this directory. For example, the package name vm, its path structure is as follows. Where demo.py is a Python script, the file name of the Python script can be customized:
datakit
└──python.d
├──vm
│├──vm.py
Write the vm.py script, fill in the following script example and change vcenter_host, vcenter_port, vcenter_user, vcenter_password to fill in the corresponding VMware host, port and user information.
Python script example
fromdatakit_frameworkimportDataKitFrameworkfrompyVim.connectimportSmartConnect,DisconnectimportrequestsfrompyVmomiimportvimimportsslimportdatetimeimporttimeclassvm(DataKitFramework):name="vm"interval=10defrun(self):print("vm")s=ssl.SSLContext(ssl.PROTOCOL_TLSv1)s.verify_mode=ssl.CERT_NONEvcenter_host="{your vcenter host}"vcenter_port="{your vcenter port}"vcenter_user="{your vcenter username}"vcenter_password="{your vcenter password}"# The data is reported to Datakit in json format, so the POST request needs to add a headheader={"Content-Type":"application/json"}# Report datadk_object=[]dk_metric=[]# Connect to the vCenter Serverc=SmartConnect(host=vcenter_host,user=vcenter_user,pwd=vcenter_password,port=vcenter_port,sslContext=s)content=c.RetrieveContent()container=content.rootFolder# starting point to look intoviewType=[vim.HostSystem,vim.VirtualMachine]# object types to look forrecursive=True# whether we should look into it recursively# Get the number and details of vCentervcenter_data=content.rootFolder.childEntityforiinvcenter_data:dk_metric.append({"measurement":"vmware","tags":{"vcenter_name":str(i)},"fields":{"vcenter_total":int(len(vcenter_data))},"time":int(time.time())})# Get the number and details of datastoredatastores=content.rootFolder.childEntity[0].datastorefordsindatastores:dk_metric.append({"measurement":"vmware","tags":{"vcenter_datastore_name":str(ds.name)},"fields":{"vcenter_datastore_total_capacity":int(ds.summary.capacity),"vcenter_datastore_total_free_space":int(ds.summary.freeSpace),"vcenter_datastore_utilization_rate":int((ds.summary.capacity-ds.summary.freeSpace)/ds.summary.capacity),"vcenter_datastore_total":int(len(datastores))},"time":int(time.time())})containerView=content.viewManager.CreateContainerView(container,viewType,recursive)children=containerView.viewforchildinchildren:ifisinstance(child,vim.HostSystem):boot_time=child.runtime.bootTimeprint("datae",datetime.datetime.now(boot_time.tzinfo)-boot_time)print("date2",int((datetime.datetime.now(boot_time.tzinfo)-boot_time).total_seconds()))dk_object.append({"measurement":"vmware_esxi","tags":{"name":str(child.name),"model":str(child.hardware.systemInfo.model),"vendor":str(child.hardware.systemInfo.vendor),"uptime":str((datetime.datetime.now(boot_time.tzinfo)-boot_time))},"fields":{"cpu_cores":str(child.hardware.cpuInfo.numCpuCores),"memory":str(child.hardware.memorySize)},"time":int(time.time())})dk_metric.append({"measurement":"vmware","tags":{"esxi_name":str(child.name),"esxi_model":str(child.hardware.systemInfo.model),"esxi_vendor":str(child.hardware.systemInfo.vendor)},"fields":{"cpu_cores":int(child.hardware.cpuInfo.numCpuCores),"memory":int(child.hardware.memorySize),"cpu_usage":int(child.summary.quickStats.overallCpuUsage),"mem_usage":int(child.summary.quickStats.overallMemoryUsage),"mem_utilization_rate":int(child.summary.quickStats.overallMemoryUsage/child.hardware.memorySize),"uptime":int((datetime.datetime.now(boot_time.tzinfo)-boot_time).total_seconds())},"time":int(time.time())})elifisinstance(child,vim.VirtualMachine):ifchild.runtime.powerState=='poweredOn':# only print VMs that are powered onboot_time=child.runtime.bootTimedk_object.append({"measurement":"vmware_vm","tags":{"name":str(child.name),"host_name":str(child.guest.hostName),"guest_os":str(child.config.guestFullName),"ip_address":str(child.guest.ipAddress),"uptime":str((datetime.datetime.now(boot_time.tzinfo)-boot_time))},"fields":{"cpu_cores":str(child.config.hardware.numCPU),"memory":str(child.config.hardware.memoryMB)},"time":int(time.time())})dk_metric.append({"measurement":"vmware","tags":{"vm_name":str(child.name),"vm_host_name":str(child.guest.hostName),"vm_ip_address":str(child.guest.ipAddress)},"fields":{"cpu_cores":int(child.config.hardware.numCPU),"memory":int(child.config.hardware.memoryMB),"cpu_usage":int(child.summary.quickStats.overallCpuUsage),"mem_usage":int(child.summary.quickStats.guestMemoryUsage),"mem_utilization_rate":int(child.summary.quickStats.guestMemoryUsage/child.config.hardware.memoryMB),"disk_usage":int(child.summary.storage.committed),"uptime":int((datetime.datetime.now(boot_time.tzinfo)-boot_time).total_seconds())},"time":int(time.time())})containerView.Destroy()in_data={'CO':dk_object,'input':'vmware_esxi,vmware_vm'}self.report(in_data)in_data2={'M':dk_metric,'input':'vmware'}self.report(in_data2)Disconnect(c)print("--------------Success---------------")