网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Saltstack之Grains组件-创新互联

参考:刘天斯《Python自动化运维技术与最佳实践》

创新互联主要从事成都网站制作、网站建设、外贸网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务加查,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

grains是Saltstack最重要的组件之一,grains的作用是收集被控主机的基本信息,这些信息通常都是一些静态类的数据,包括CPU、内核、操作系统、虚拟化等,在服务器端可以根据这些信息进行灵活定制,管理员可以利用这些信息对不同业务进行个性化定制。

获取所有主机的grains项信息,发现没有max_openfile,等会我们手动编写一个。

[root@salt-master base]# salt '*' grains.ls
salt-master:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gid
    - gpus
    - groupname
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - pid
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - systemd
    - uid
    - username
    - uuid
    - virtual
    - zmqversion
salt-minion01:
    - SSDs
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gid
    - gpus
    - groupname
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - master
    - max_open_file
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - pid
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - server_id
    - shell
    - uid
    - username
    - virtual
    - zmqversion
hddcluster2:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gid
    - gpus
    - groupname
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - pid
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - systemd
    - uid
    - username
    - uuid
    - virtual
    - zmqversion
hddcluster4:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gid
    - gpus
    - groupname
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - pid
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - systemd
    - uid
    - username
    - uuid
    - virtual
    - zmqversion
hddcluster3:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gid
    - gpus
    - groupname
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - pid
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - systemd
    - uid
    - username
    - uuid
    - virtual
    - zmqversion
hddcluster1:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gid
    - gpus
    - groupname
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - pid
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - systemd
    - uid
    - username
    - uuid
    - virtual
    - zmqversio

定义grains数据的方法有两种,一种为在被控主机定制配置文件:另一种是通过主控端扩展模块API实现。

我这这里直说主控端,毕竟我们要搞自动化运维,需要批量操作。

主控端扩展模块定制grains数据
vim /etc/salt/master
file_roots:
  base:                   //基础平台
    - /srv/salt/base
  prod:                   //生产平台
    - /srv/salt/prod
	
install -d /srv/salt/base/_grains	
vim /srv/salt/base/_grains/grains_openfile.py      //编写一个python脚本获取max_openfile的值
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os,sys,commands

#定义一个获取大打开文件数的函数,函数名称没有要求,符合python函数命名规则即可
def Grains_openfile():
    '''
        return os max open file of grains value
    '''
    grains = {} #初始化一个字典,变量名一定要用grains,以便Saltstack识别

    _open_file=65535 #初始化一个默认值

    try:
        getulimit = commands.getstatusoutput('source /etc/profile;ulimit -n')
    except Exception,e:
        pass

    if getulimit[0]==0:
        _open_file=int(getulimit[1])
    grains['max_open_file'] = _open_file #将获取的ulimit -n的结果进行赋值,其中'max_open_file'就是grains项,——open_file就是grains的值
    return grains	

最后同步模块到指定被控主机并刷新生效,因为grains比较适合采集静态类的数据,比如硬件、内核信息等,当有动态类的功能需求时,需要进行刷新。
同步操作:

服务端master
[root@salt-master base]# salt 'salt-minion01' saltutil.sync_all 
salt-minion01:
    ----------
    beacons:
    engines:
    grains:
        - grains.grains_openfile
    log_handlers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:	
	
客户端minion
结果:文件同步到被控端的cache目录中
[root@salt-minion01 ~]# find / -type f -name grains_openfile.py 
/var/cache/salt/minion/files/base/_grains/grains_openfile.py
/var/cache/salt/minion/extmods/grains/grains_openfile.py	
#注:/var/cache/salt/minion/extmods/grains/为扩展模块文件最终存放位置,刷新模块后将在同路径下生产字节码pyc;/var/cache/salt/minion/files/base/_grains/为临时存放位置。
刷新操作:
[root@salt-master base]# salt 'salt-minion01' sys.reload_modules
salt-minion01:
    True
	
[root@salt-minion01 ~]# find / -type f -name grains_openfile.py*
/var/cache/salt/minion/files/base/_grains/grains_openfile.py
/var/cache/salt/minion/extmods/grains/grains_openfile.pyc    //多了一个pyc
/var/cache/salt/minion/extmods/grains/grains_openfile.py
查看同步后是否能正常获取数据
[root@salt-master base]# salt 'salt-minion01' grains.item max_open_file
salt-minion01:
    ----------
    max_open_file:
        1024
		
其他未同步的,并不会有max_open_file数据		
[root@salt-master base]# salt '*' grains.item max_open_file             
salt-master:
    ----------
    max_open_file:
salt-minion01:
    ----------
    max_open_file:
        1024
hddcluster2:
    ----------
    max_open_file:
hddcluster4:
    ----------
    max_open_file:
hddcluster3:
    ----------
    max_open_file:
hddcluster1:
    ----------
    max_open_file:
[root@salt-master base]# 	

其他未同步的,并不会有max数据,也没有这个模块	
[root@salt-master base]# salt '*' grains.item max
salt-master:
    ----------
    max:
hddcluster2:
    ----------
    max:
salt-minion01:
    ----------
    max:
hddcluster3:
    ----------
    max:
hddcluster4:
    ----------
    max:
hddcluster1:
    ----------
    max:

最后再试一下吧,同步所有机器,再刷新所有机器

[root@salt-master base]# salt '*' saltutil.sync_all               
salt-master:
    ----------
    beacons:
    engines:
    grains:
        - grains.grains_openfile
    log_handlers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:
salt-minion01:                //这个同步过了,没有更新
    ----------
    beacons:
    engines:
    grains:
    log_handlers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:
hddcluster2:
    ----------
    beacons:
    engines:
    grains:
        - grains.grains_openfile
    log_handlers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:
hddcluster3:
    ----------
    beacons:
    engines:
    grains:
        - grains.grains_openfile
    log_handlers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:
hddcluster4:
    ----------
    beacons:
    engines:
    grains:
        - grains.grains_openfile
    log_handlers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:
hddcluster1:
    ----------
    beacons:
    engines:
    grains:
        - grains.grains_openfile
    log_handlers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    states:
    utils:
[root@salt-master base]# salt '*' sys.reload_modules                    
salt-master:
    True
salt-minion01:
    True
hddcluster2:
    True
hddcluster4:
    True
hddcluster3:
    True
hddcluster1:
    True
[root@salt-master base]# salt '*' grains.item max_open_file
salt-master:
    ----------
    max_open_file:
        8192
hddcluster2:
    ----------
    max_open_file:
salt-minion01:
    ----------
    max_open_file:
        1024
hddcluster3:
    ----------
    max_open_file:
        8192
hddcluster4:
    ----------
    max_open_file:
        8192
hddcluster1:
    ----------
    max_open_file:
        8192

至此,已经测试完毕,需要写其他模块,可以在_grains目录下继续添加。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:Saltstack之Grains组件-创新互联
文章链接:http://bjjierui.cn/article/jhcoe.html

其他资讯