1、查看MASTER的BASE路径,在对应的路径建立_modules文件夹,存放.py的模块

[[email protected] _modules]# cat /etc/salt/master | grep file_roots -A 5
--
file_roots:
  base:
    - /srv/salt

 

建立目录:

mkdir -p /srv/salt/_modules

2、在/srv/salt/_modules目录下编写python代码

[[email protected] _modules]# pwd
/srv/salt/_modules
[[email protected] _modules]# cat infos.py 
#!/usr/bin/env python
#encoding: utf-8
from subprocess import Popen, PIPE
import os,sys
import time
def getipaddr():
    p = Popen(['hostname','-I'], stdout = PIPE)
    ip=p.stdout.read()
    return ip

def gethostname():
    p = Popen(['hostname'], stdout = PIPE)
    hostname=p.stdout.read()
    return hostname

def getdiskinfo():
    p = Popen(['df','-h'], stdout = PIPE)
    disks=p.stdout.read()
    return disks

def getmemoryinfo():
    p = Popen(['free','-m'], stdout = PIPE)
    memorys=p.stdout.read()
    return memorys

def getbiosinfo():
    p = Popen(['dmidecode','-t','bios'], stdout = PIPE)
    bios=p.stdout.read()
    return bios

def getcpu():
    p = Popen(['sar','-u','1','3'], stdout = PIPE)
    time.sleep(3)
    cpus=p.stdout.read()
    return cpus

def getallinfos():
    ip="IP地址: "+getipaddr()
    hostname="主机名: "+gethostname()
    disk="磁盘信息: "+getdiskinfo()
    memory="内存信息: "+getmemoryinfo()
    bios="BIOS信息: "+getbiosinfo()
    cpu="CPU信息: "+getcpu()
    return ip,hostname,disk,memory,bios,cpu
#getallinfos()

3、同步模块,执行验证

[[email protected] _modules]# salt '*' saltutil.sync_modules
v72:
    - modules.infos
v73:
    - modules.infos

运行,验证

[[email protected] _modules]# salt '*' infos.getallinfos    
v73:
    - IP地址: 192.168.156.73 192.168.122.1 
    - 主机名: v73
    - 磁盘信息: 文件系统                 容量  已用  可用 已用% 挂载点
      /dev/mapper/centos-root   18G  8.0G  9.5G   46% /
      devtmpfs                 903M     0  903M    0% /dev
      tmpfs                    914M   92K  914M    1% /dev/shm
      tmpfs                    914M  9.1M  905M    1% /run
      tmpfs                    914M     0  914M    0% /sys/fs/cgroup
      /dev/sda1                497M  127M  370M   26% /boot
      //192.168.156.1/d$       201G   98G  103G   49% /shared
      tmpfs                    183M   12K  183M    1% /run/user/42
      tmpfs                    183M     0  183M    0% /run/user/0
    - 内存信息:               total        used        free      shared  buff/cache   available
      Mem:           1826         389         357           9        1079        1201
      Swap:          2047           0        2047
    - BIOS信息: # dmidecode 2.12
      SMBIOS 2.7 present.
      
      Handle 0x0000, DMI type 0, 24 bytes
      BIOS Information
        Vendor: Phoenix Technologies LTD
        Version: 6.00
        Release Date: 07/02/2015
        Address: 0xEA5E0
        Runtime Size: 88608 bytes
        ROM Size: 64 kB
        Characteristics:
                ISA is supported
                PCI is supported
                PC Card (PCMCIA) is supported
                PNP is supported
                APM is supported
                BIOS is upgradeable
                BIOS shadowing is allowed
                ESCD support is available
                Boot from CD is supported
                Selectable boot is supported
                EDD is supported
                Print screen service is supported (int 5h)
                8042 keyboard services are supported (int 9h)
                Serial services are supported (int 14h)
                Printer services are supported (int 17h)
                CGA/mono video services are supported (int 10h)
                ACPI is supported
                Smart battery is supported
                BIOS boot specification is supported
                Function key-initiated network boot is supported
                Targeted content distribution is supported
        BIOS Revision: 4.6
        Firmware Revision: 0.0
      
    - CPU信息: Linux 3.10.0-229.el7.x86_64 (v73)        2019年02月22日  _x86_64_        (1 CPU)
      
      17时06分18秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
      17时06分19秒     all      0.00      0.00      0.00      0.00      0.00    100.00
      17时06分20秒     all      1.00      0.00      0.00      0.00      0.00     99.00
      17时06分21秒     all      0.00      0.00      0.00      0.00      0.00    100.00
      平均时间:     all      0.33      0.00      0.00      0.00      0.00     99.67
v72:
    - IP地址: 192.168.156.72 192.168.122.1 
    - 主机名: v72
    - 磁盘信息: 文件系统                 容量  已用  可用 已用% 挂载点
      /dev/mapper/centos-root   18G   12G  5.7G   68% /
      devtmpfs                 903M     0  903M    0% /dev
      tmpfs                    914M   96K  914M    1% /dev/shm
      tmpfs                    914M  9.2M  905M    2% /run
      tmpfs                    914M     0  914M    0% /sys/fs/cgroup
      /dev/sda1                497M  127M  370M   26% /boot
      //192.168.156.1/d$       201G   98G  103G   49% /shared
      tmpfs                    183M   12K  183M    1% /run/user/42
      tmpfs                    183M     0  183M    0% /run/user/0
    - 内存信息:               total        used        free      shared  buff/cache   available
      Mem:           1826         719         316          10         791         871
      Swap:          2047           0        2047
    - BIOS信息: # dmidecode 2.12
      SMBIOS 2.7 present.
      
      Handle 0x0000, DMI type 0, 24 bytes
      BIOS Information
        Vendor: Phoenix Technologies LTD
        Version: 6.00
        Release Date: 07/02/2015
        Address: 0xEA5E0
        Runtime Size: 88608 bytes
        ROM Size: 64 kB
        Characteristics:
                ISA is supported
                PCI is supported
                PC Card (PCMCIA) is supported
                PNP is supported
                APM is supported
                BIOS is upgradeable
                BIOS shadowing is allowed
                ESCD support is available
                Boot from CD is supported
                Selectable boot is supported
                EDD is supported
                Print screen service is supported (int 5h)
                8042 keyboard services are supported (int 9h)
                Serial services are supported (int 14h)
                Printer services are supported (int 17h)
                CGA/mono video services are supported (int 10h)
                ACPI is supported
                Smart battery is supported
                BIOS boot specification is supported
                Function key-initiated network boot is supported
                Targeted content distribution is supported
        BIOS Revision: 4.6
        Firmware Revision: 0.0
      
    - CPU信息: Linux 3.10.0-229.el7.x86_64 (v72)        2019年02月22日  _x86_64_        (2 CPU)
      
      17时06分20秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
      17时06分21秒     all      0.00      0.00      0.00      0.00      0.00    100.00
      17时06分22秒     all      0.51      0.00      0.00      0.00      0.00     99.49
      17时06分23秒     all      0.00      0.00      0.51      0.00      0.00     99.49
      平均时间:     all      0.17      0.00      0.17      0.00      0.00     99.66

 

加载哪一部份的信息,只需要在saltstack上面调用对应的方法就可以了

使用saltstack自定义模块获取服务器的信息

 

将来如果需要获取其它的信息,只需要在自定义的模块添加对应的方法,同步,再执行即可

[[email protected] _modules]# cat infos.py | grep getos -A 5
def getos():
    status,output=commands.getstatusoutput("uname -a")
    return output
 

注意上面的方法也和上面一样可以获取linux命令的输出,但需要先导入模块

import commands

 

使用saltstack自定义模块获取服务器的信息

 

 

 

相关文章: