Development CMDB, in addition to record the server's account password, but also need to record the server hardware and software information, if the use of manual records, if the server is large, the workload is relatively large, so I need to be able to automatically obtain this information method, I used the ansible to get, Ansible with the Setup module, you can get this information to the server, and then from this information to filter out the information I need, storage can be.
I probably need the following information:
Physical memory capacity
Virtual content Capacity
CPU model
CPU Core Number
Operating system Type
Total hard Drive capacity
Hard disk mount name and capacity
Server model
Server Host Name
OS kernel model
Server IPv4 Address
In the operational tools, I have integrated this automatic access information function, just click on the mouse "update", will go to get the information, and then write this information to the database. Front-end Template display:
Operation Dimension Development: CDMB Front-End Template
After the success of the update (get the information, the front end of the display, later I click on the "Details" button to show):
Operational dimension development: CMDB Front-End Update success
Look at the information that the database gets:
Mysql> SELECT * from Yw_device \g;
1. Row ***************************
Id:1
Name:app2
Line: Telecom
wip:192.168.1.6
nip:192.168.1.6
Username:root
Password:xxxxxxxx
Port:22
Key_path:
Room_id:1
Type_id:1
status:0
mem_total:32058
swap_total:4095
Cpu_type:intel (R) Xeon (r) CPU e5-2407 v2 @ 2.40GHz
Cpu_total:4
Os_type:centos 6.4
disk_total:1820
Disk_mount: [{' Mount ': '/', ' Size ': 1917}, {' Mount ': '/boot ', ' Size ': 0}, {' Mount ': '/home/postgres/archive ', ' Size ': 1917 }]
Server_type:poweredge R420
Host_name:dywl
Os_kernel:2.6.32-504.12.2.el6.x86_64
IPv4: [' 192.168.1.6 ', ' 192.168.1.9 ', ' 192.168.1.12 ', ' 10.8.0.1 ']
2. Row ***************************
CMDB Hardware Information Details
Principle:
Front click "Update", with the Ajax method, the server's IP, account number, password, port in the post way to access my/api/getmachineinfo/interface, this interface received the request, take these server information call Ansible API, The information returned from the ansible is then retrieved and then written to the database and returned to the "successful operation" information.
Front-End related HTML code:
' <a href= ' # "onclick=" refresh (\ ' + row[' id '] + ' "class=" Btn-sm ">",
' <i class= ' fa fa-refresh ' > Update </i> ',
' </a> '
Front-End-related AJAX code
function Refresh (ID) {
var data = $ (' #myLoadTable '). Bootstraptable (' Getrowbyuniqueid ', id);
$.ajax ({
Type: ' Post ',
DataType: ' JSON ',
URL: "/api/getmachineinfo/",
Submission of data
data:{
"username":d ata[' username '],
"Password":d ata[' password '],
"WIP":d ata[' WIP '],
"Port":d ata[' Port '],
"Key_path":d ata[' Key_path ']
},
//
Beforesend:function () {
Layer.load (1, {
Shade: [0.1, ' #222 ']//0.1 white background of transparency
});
},
function called after successful return
Success:function (data) {
var message = data[' resmsg '];
if (data[' resid '] = = ' 0 ') {
Refreshing data
LAYER.MSG (message, {icon:1, time:1000});
} else {
LAYER.MSG (message, {icon:2, time:1500});
}
},
function called after the call was executed
Complete:function () {
Layer.closeall (' loading ');
}
});
}
Back-end Code (/api/getmachineinfo/)
#!/usr/bin/evn python
# Coding=utf-8
__author__ = ' Dairu '
Import logging
From bottle Import Post
From Controller.core import Ansible_api, remsg, Checkargs
From controller.db import Writedb
@post ('/api/getmachineinfo/')
DEF callback ():
"""
Use Ansible's setup module to obtain server-related information, such as hardware information, operating system information, etc.
"""
R_IP = Checkargs ("WIP", "Public network IP")
R_user = Checkargs ("username", "Server account")
R_pass = Checkargs ("password", "Server password")
R_port = Checkargs ("Port", "Remote Ports")
Key_path = Checkargs ("Key_path", "Server Key", False)
R_name = "Setup" # Use Ansible's module name
R_ARGV = "" # The command arguments passed to the Ansible API, because Setup is not required to pass parameters to setup.
# Call the Ansible API to get data remotely
data = Ansible_api (R_name, R_ARGV, R_ip, R_user, R_pass, R_port, Key_path, Forks_num=1)
Try
data = data["Ansible_facts"]
Except Exception, E:
Logging.error (' ansible Setup module failed to get data:%s '% e)
Return Remsg (-1, "Remote server gets information failed")
# Physical Memory capacity
Try
Mem_total = data["ANSIBLE_MEMTOTAL_MB"]
Except Exception, E:
Logging.error (' ansible Setup module failed to get physical memory capacity data:%s '% e)
Mem_total = 0
# Virtual Content Capacity
Try
Swap_total = data["ANSIBLE_MEMORY_MB" ["Swap"] ["Total"]
Except Exception, E:
Logging.error (' ansible Setup module failed to get virtual content capacity data:%s '% e)
Swap_total = 0
# CPU Model
Try
CPU_type = data["Ansible_processor"][-1]
Except Exception, E:
Logging.error (' ansible Setup module failed to get CPU model data:%s '% e)
CPU_type = "Unknown"
# CPU Core Number
Try
Cpu_total = data["Ansible_processor_vcpus"]
Except Exception, E:
Logging.error (' ansible Setup module failed to get CPU core data:%s '% e)
Cpu_total = 0
# Operating system Type
Try
Os_type = "". Join ((data["ansible_distribution"], data["ansible_distribution_version"))
Except Exception, E:
Logging.error (' ansible Setup module failed to get OS type data:%s '% e)
Os_type = "Unknown"
# Hard Drive Total capacity
Try
Disk_total = SUM ([Int (data["ansible_devices"][i]["sectors"]) * \
Int (data["ansible_devices"][i]["SectorSize"])/1024/1024/1024 \
For i in data["ansible_devices"] if I[0:2] in ("SD", "SS")])
Except Exception, E:
Logging.error (' ansible setup module gets hard drive total capacity data failed:%s '% e)
Disk_total = 0
# hard disk mount name and capacity
Try
Disk_mount = STR (
[{"Mount": i["Mount"], "size": i["Size_total"]/1024/1024/1024} for I in data["ansible_mounts"])
Except Exception, E:
Logging.error (' ansible setup module gets hard drive mount name and capacity data failed:%s '% e)
Disk_mount = "Unknown"
# Server Model
Try
Server_type = data["Ansible_product_name"]
Except Exception, E:
Logging.error (' ansible Setup module failed to get server model data:%s '% e)
Server_type = "Unknown"
# Server Host Name
Try
HOST_NAME = data["Ansible_hostname"]
Except Exception, E:
Logging.error (' ansible Setup module failed to get server host name data:%s '% e)
HOST_NAME = "Unknown"
# Operating System Kernel model
Try
Os_kernel = data["Ansible_kernel"]
Except Exception, E:
Logging.error (' ansible Setup module failed to get OS kernel model data:%s '% e)
Os_kernel = "Unknown"
# Server IPv4 Address
Try
IPv4 = str (data["ansible_all_ipv4_addresses"])
Except Exception, E:
Logging.error (' ansible Setup module failed to get server IPv4 address data:%s '% e)
IPv4 = "Unknown"
# Update Database
sql = "UPDATE yw_device SET Mem_total=%s,swap_total=%s,cpu_type=%s,cpu_total=%s,os_type=%s,disk_total=%s,disk_mount =%s,\
server_type=%s,host_name=%s,os_kernel=%s,ipv4=%s WHERE wip=%s "
data = (
Mem_total, Swap_total, CPU_type, Cpu_total, Os_type, Disk_total, Disk_mount, Server_type, host_name, Os_kernel,
IPv4, R_IP)
result = Writedb (SQL, data)
# Determine if the operation database is successful and return information
If result:
Return remsg (0, "Update successful")
Return Remsg (-1, "Update Failed")