rms_py

  • 获取服务器信息脚本
      1 import urllib2
      2 import re
      3 import json
      4 import subprocess
      5 import cookielib
      6 import time
      7 import socket
      8 import os
      9 from rms_functions_linux import *
     10 api_token='cee48674b45d2acc54476382c294295d'
     11 
     12 fnull = open(os.devnull, 'w')
     13 net_result = subprocess.call('ping -c 1 x.x.x.x', shell = True, stdout = fnull, stderr = fnull)
     14 if net_result:
     15     download_ip='192.168.1.x'
     16     Server='10.100.1.x'
     17 else:
     18     download_ip='x.x.x.x'
     19     Server='agent.rms.ops.hunantv.com'
     20 
     21 file_path = os.path.split(os.path.realpath(__file__))[0]
     22 
     23 out_main_url=' -u "imogo_ops:ULumo$=av%%(tFO" -H "HOST:os-manage.ops.imogo.com"  -k  https://%s:8443/os-manage/scripts/get_linux_server_info.py -o %s/get_linux_
     24 server_info.py'%(download_ip, file_path)
     25 out_fuc_url='  -u "imogo_ops:ULumo$=av%%(tFO" -H "HOST:os-manage.ops.imogo.com"  -k  https://%s:8443/os-manage/scripts/rms_functions_linux.py -o %s/rms_function
     26 s_linux.py'%(download_ip, file_path)
     27 check_main_url=' -u "imogo_ops:ULumo$=av%%(tFO" -H "HOST:os-manage.ops.imogo.com"  -k  https://%s:8443/os-manage/scripts/get_linux_server_info.md5'%(download_ip
     28 )
     29 check_fuc_url='  -u "imogo_ops:ULumo$=av%%(tFO" -H "HOST:os-manage.ops.imogo.com"  -k  https://%s:8443/os-manage/scripts/rms_functions_linux.md5'%(download_ip)
     30 
     31 post_server_info_url='http://%s/hrm/server/agent_server_post?token=%s'
     32 
     33 
     34 def boot_dev(dev):
     35     cmd='/usr/bin/ipmitool'
     36     ipmi_params='-I open chassis bootdev %s' % dev
     37     ipmi_info=get_cmd_info(cmd,ipmi_params)
     38 
     39 def get_server_status(rms,url,server_sn):
     40     surl=url % (Server,server_sn,api_token)
     41     result=rms.get(surl)
     42     all_result={}
     43     if result:
     44         sdict=json.loads(result)
     45         all_result['server_hard_status'] = True
     46         if sdict['status'] == 0:
     47             all_result['server_soft_status'] = True
     48         else:
     49             all_result['server_soft_status'] = None
     50     else:
     51         all_result['server_hard_status'] = None
     52     return all_result
     53 
     54 def post_server_info(rms,url,post_data):
     55     surl=url % (Server,api_token)
     56     for i in range(5):
     57         result=rms.post(surl,post_data)
     58         time.sleep(5)
     59         if  result:
     60             print result.read()
     61             break
     62 
     63 def check_update():
     64     cmd = "md5sum %s/get_linux_server_info.py | awk  '{print $1}'"%file_path
     65     file = os.popen(cmd)
     66     file_get_md5 = file.read().split("\n")[0]
     67     cmd = "md5sum %s/rms_functions_linux.py | awk  '{print $1}'"%file_path
     68     file = os.popen(cmd)
     69     file_rms_md5 = file.read().split("\n")[0]
     70     server_get_md5 = os.popen("curl -s" + check_main_url).read()
     71     server_rms_md5 = os.popen("curl -s" + check_fuc_url).read()
     72     if server_get_md5 != file_get_md5:
     73         os.system('rm -rf get_linux_server_info.py')
     74         os.system('rm -rf get_linux_server_info.pyc')
     75         os.system("curl -s" + out_main_url)
     76     if server_rms_md5 != file_rms_md5:
     77         os.system('rm -rf rms_functions_linux.py')
     78         os.system('rm -rf rms_functions_linux.pyc')
     79         os.system("curl -s" + out_fuc_url)
     80 
     81 
     82 
     83 def start_lldpad():
     84     lldpad_cmd='/etc/init.d/lldpad'
     85     lldpad_params='restart'
     86     result=get_cmd_info(lldpad_cmd,lldpad_params)
     87 
     88 
     89 
     90 def main():
     91     check_update()
     92     start_lldpad()
     93     time.sleep(10)
     94     post_params={'sn':'',
     95                  'mac':'',
     96                  'cpu':'',
     97                  'memory':'',
     98                  'disks':'',
     99                  'vendor':'',
    100                  'switch':'',
    101                  'os': ''
    102                 }
    103     raid_config = check_raid_vender()
    104     if not raid_config:
    105         print "error: no raid config found"
    106         #sys.exit(1)
    107     server_sn=get_server_sn()
    108     server_mac=get_server_NIC_info()[0][1]
    109     cpu_info=get_cpu_info()
    110     dlist=get_disk_info(raid_config)
    111     post_data_list=[]
    112     os_info=get_os_info()
    113     if dlist:
    114         disk_info=format_disk(dlist)
    115     else:
    116         disk_info=get_single_disk()
    117     memory_info=get_memory_info()
    118     nic_info=get_server_NIC_info()
    119     switch_info=get_switch_info(nic_info)
    120     server_vendor_info= get_server_fullvender()
    121     post_params['mac']=server_mac
    122     post_params['os']=os_info
    123     post_params['sn']=server_sn
    124     post_params['memory']=memory_info
    125     post_params['cpu']=cpu_info
    126     post_params['disks']=disk_info
    127     post_params['switch']=switch_info
    128     post_params['vendor']=server_vendor_info
    129     post_params.update(get_server_ip_info())
    130     post_params.update({'hostname':socket.gethostname()})
    131     post_data_list.append(post_params)
    132     rms=rms_https()
    133     post_server_info(rms,post_server_info_url,post_data_list)
    134 
    135 
    136 
    137 if __name__ == '__main__':
    138     main()
    get_server_info.py

     

posted on 2016-09-02 16:45  不想做分母  阅读(104)  评论(0)    收藏  举报

导航