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

本文介绍了如何在SaltStack中创建自定义模块,并通过示例展示了如何获取主机信息,包括IP地址、主机名、磁盘、内存、BIOS和CPU信息。通过saltutil.sync_modules命令同步模块,并验证了模块的正确执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

[root@v72 _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代码

[root@v72 _modules]# pwd

/srv/salt/_modules

[root@v72 _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、同步模块,执行验证

[root@v72 _modules]# salt '*' saltutil.sync_modules

v72:

- modules.infos

v73:

- modules.infos

运行,验证

[root@v72 _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上面调用对应的方法就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值