关于saltstack mine自定义收集minion状态及应用场景

简介:

这两天有人问我saltstack 关于mine的主要应用场景和具体的用法,我翻了下自己一些写的关于saltstack的博客,还真没有mine的文章。。。 现在总结下mine的文档,供大家拍砖。


mine是做什么?   就是你在mine定义了收集模块,他自己会把数据收集的,存到了哪里,不知道,但是肯定是客户端minino本地。有兴趣的朋友可以check saltstack的代码。


这里先粘贴下,官方的doc用法:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
mine.delete:                                                                           
                                                                                        
     Remove specific  function  contents of minion. Returns True on success.              
                                                                                        
     CLI Example:                                                                       
                                                                                        
         salt  '*'  mine.delete  'network.interfaces'                                      
                                                                                        
                                                                                        
mine.flush:                                                                            
                                                                                        
     Remove all mine contents of minion. Returns True on success.                       
                                                                                        
     CLI Example:                                                                       
                                                                                        
         salt  '*'  mine.flush                                                            
                                                                                        
                                                                                        
mine.get:                                                                              
                                                                                        
     Get data from the mine based on the target,  function  and expr_form                 
                                                                                        
     Targets can be matched based on any standard matching system that can be           
     matched on the master via these keywords::                                         
                                                                                        
         glob                                                                           
         pcre                                                                           
         grain                                                                          
         grain_pcre                                                                     
                                                                                        
     CLI Example:                                                                       
                                                                                        
         salt  '*'  mine.get  '*'  network.interfaces                                       
         salt  '*'  mine.get  'os:Fedora'  network.interfaces grain                         
                                                                                        
                                                                                        
mine.send:                                                                             
                                                                                        
     Send a specific  function  to the mine.                                              
                                                                                        
     CLI Example:                                                                       
                                                                                        
         salt  '*'  mine.send network.interfaces eth0                                     
                                                                                        
                                                                                        
mine.update:                                                           原文:               
                                                                                        
     Execute the configured functions and send the data back up to the master           
     The functions to be executed are merged from the master config, pillar and         
     minion config under the option  "function_cache" :                                   
                                                                                        
         mine_functions:                                                                
           network.ip_addrs:


配置起来很简单,跑到minion端,配置下文件,开启几个模块的检测。


[root@vm-10-154-252-46 utils]# tail /etc/salt/minion

mine_functions:

  test.ping: []

  network.dig:

   - www.naver.com

  network.usage: []


mine_interval: 1



然后咱们跑到master端去抓数据。


[root@vm-10-154-252-82 ~/clusterops/templates]$salt '10.154.252.46' mine.get '*' network.dig           

10.154.252.46:                                                                                         

    ----------                                                                                         

    10.154.252.46:                                                                                     

                                                                                                       

        ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> www.baidu.com                               

        ;; global options: +cmd                                                                        

        ;; Got answer:                                                                                 

        ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5279                                       

        ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 2                           

                                                                                                       

        ;; QUESTION SECTION:                                                                           

        ;www.baidu.com.                 IN      A                                                      

                                                                                                       

        ;; ANSWER SECTION:                                                                             

        www.baidu.com.          186     IN      CNAME   www.a.shifen.com.                              

        www.a.shifen.com.       149     IN      A       220.181.111.188                                

        www.a.shifen.com.       149     IN      A       220.181.112.244                                

                                                                                                       

        ;; AUTHORITY SECTION:                                                                          

        a.shifen.com.           186     IN      NS      ns3.a.shifen.com.                              

        a.shifen.com.           186     IN      NS      ns1.a.shifen.com.                              

        a.shifen.com.           186     IN      NS      ns5.a.shifen.com.                              

        a.shifen.com.           186     IN      NS      ns2.a.shifen.com.                              

        a.shifen.com.           186     IN      NS      ns4.a.shifen.com.                              

                                                                                                       

        ;; ADDITIONAL SECTION:                                                                         

        ns5.a.shifen.com.       70      IN      A       119.75.219.43                                  

        ns2.a.shifen.com.       186     IN      A       180.149.133.241                                

                                                                                                       

        ;; Query time: 1 msec                                                                          

        ;; SERVER: 10.150.0.253#53(10.150.0.253)                                                       

        ;; WHEN: Wed Jul  2 14:42:55 2014                                                              

        ;; MSG SIZE  rcvd: 212                                                                         


原文: http://rfyiamcool.blog.51cto.com/1030776/1433843

[root@vm-10-154-252-82 ~/clusterops/templates]$                                                        


我先前定义的是一分钟来生成一次数据。在minon的日志端是可以体现出来的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2014-07-03 09:15:05,054 [salt.loaded.int.module.mine 
2014-07-03 09:15:05,186 [salt.loaded.int.module.mine 
2014-07-03 09:16:05,149 [salt.loaded.int.module.mine 
2014-07-03 09:16:05,274 [salt.loaded.int.module.mine 
2014-07-03 09:17:05,242 [salt.loaded.int.module.mine 
2014-07-03 09:17:05,366 [salt.loaded.int.module.mine 
2014-07-03 09:18:05,336 [salt.loaded.int.module.mine 
2014-07-03 09:18:05,463 [salt.loaded.int.module.mine 
2014-07-03 09:19:05,430 [salt.loaded.int.module.mine 
2014-07-03 09:19:05,554 [salt.loaded.int.module.mine 
2014-07-03 09:20:05,523 [salt.loaded.int.module.mine 
2014-07-03 09:20:05,648 [salt.loaded.int.module.mine 
2014-07-03 09:21:05,619 [salt.loaded.int.module.mine 
2014-07-03 09:21:05,748 [salt.loaded.int.module.mine 
2014-07-03 09:22:05,713 [salt.loaded.int.module.mine 
2014-07-03 09:22:05,843 [salt.loaded.int.module.mine 
2014-07-03 09:23:05,808 [salt.loaded.int.module.mine 
2014-07-03 09:23:05,938 [salt.loaded.int.module.mine 
2014-07-03 09:24:05,901 [salt.loaded.int.module.mine 
2014-07-03 09:24:06,026 [salt.loaded.int.module.mine 
2014-07-03 09:25:05,995 [salt.loaded.int.module.mine 
2014-07-03 09:25:06,120 [salt.loaded.int.module.mine 
2014-07-03 09:26:05,088 [salt.loaded.int.module.mine 
2014-07-03 09:26:05,214 [salt.loaded.int.module.mine 
2014-07-03 09:27:05,183 [salt.loaded.int.module.mine 
2014-07-03 09:27:05,319 [salt.loaded.int.module.mine 
2014-07-03 09:28:05,287 [salt.loaded.int.module.mine 
2014-07-03 09:28:05,418 [salt.loaded.int.module.mine 
2014-07-03 09:29:05,373 [salt.loaded.int.module.mine 
2014-07-03 09:29:05,502 [salt.loaded.int.module.mine 
2014-07-03 09:30:05,466 [salt.loaded.int.module.mine 
2014-07-03 09:30:05,591 [salt.loaded.int.module.mine 
2014-07-03 09:31:05,560 [salt.loaded.int.module.mine 
2014-07-03 09:31:05,689 [salt.loaded.int.module.mine 
2014-07-03 09:32:05,655 [salt.loaded.int.module.mine 
2014-07-03 09:32:05,785 [salt.loaded.int.module.mine 
2014-07-03 09:33:05,749 [salt.loaded.int.module.mine 
2014-07-03 09:33:05,874 [salt.loaded.int.module.mine 
2014-07-03 09:34:05,845 [salt.loaded.int.module.mine 
2014-07-03 09:34:05,969 [salt.loaded.int.module.mine 
2014-07-03 09:35:05,937 [salt.loaded.int.module.mine 
2014-07-03 09:35:06,068 [salt.loaded.int.module.mine 
2014-07-03 09:36:05,017 [salt.loaded.int.module.mine 
2014-07-03 09:36:05,141 [salt.loaded.int.module.mine 
2014-07-03 09:37:05,113 [salt.loaded.int.module.mine 
2014-07-03 09:37:05,238 [salt.loaded.int.module.mine 
2014-07-03 09:38:05,208 [salt.loaded.int.module.mine 
2014-07-03 09:38:05,336 [salt.loaded.int.module.mine 
2014-07-03 09:39:05,304 [salt.loaded.int.module.mine 
2014-07-03 09:39:05,428 [salt.loaded.int.module.mine 
2014-07-03 09:40:05,687 [salt.loaded.int.module.mine 
2014-07-03 09:40:05,810 [salt.loaded.int.module.mine 
2014-07-03 09:41:05,781 [salt.loaded.int.module.mine 
2014-07-03 09:41:05,908 [salt.loaded.int.module.mine



咱们也可以利用mine.send来发送数据。


[root@vm-10-154-252-82 ~/clusterops/templates]$salt '10.154.252.46' mine.send network.dig youku.com       

10.154.252.46:                                                                                            

    True                                                                                                  

[root@vm-10-154-252-82 ~/clusterops/templates]$                                                           


我们又重新定义了mine的规则。

1
2
3
4
mine_functions:
   test . ping : []
   cmd.run:
    date ; df


咱们定义了有时间特征的收集,用来判断下,他获取的数据是不是一分钟间隔,你就算mine.get也只是获取到他先前生成的数据,而不是实时的。

原文:http://rfyiamcool.blog.51cto.com/1030776/1433843


wKioL1O0uCnAVsd2AAaAeLOLDHM598.jpg


wKioL1O0uKrTwCaUAALHxCHZpTM407.jpg


有些朋友还是再疑惑这些东西也可以用直接推送模块来实现,但是你看下面,我定义了sleep 10s,如果用模块去取值的话,一定要sleep 10秒之后,才能获取到数据。但这里master去get数据的时候,还是很即时的回来。当然取得数据是一分钟之前的。对一些场合这已经足够了。



wKiom1O0vU-y66T8AAQGxHTqjiE955.jpg



上面说了,是什么场合?   来个简单渲染haproxy配置。 

/srv/pillar/top.sls:

base:

  'G@roles:web':

    - web

原文:http://rfyiamcool.blog.51cto.com/1030776/1433843

/srv/pillar/web.sls:

mine_functions:

  network.ip_addrs: [eth0]


/etc/salt/minion.d/mine.conf:

mine_interval: 5


/srv/salt/haproxy.sls:

haproxy_config:

  file:

    - managed

    - name: /etc/haproxy/config

    - source: salt://haproxy_config

    - template: jinja


/srv/salt/haproxy_config:

<...file contents snipped...>


{% for server, addrs in salt['mine.get']('roles:web', 'network.ip_addrs', expr_form='grain').items() %}

server ` server ` {{ addrs[0] }}:80 check

{% endfor %}


<...file contents snipped...>



根据monin客户端的一些特征来渲染配置,估计大家已经学会了用根据grains取渲染配置,但是grains的数据有些不及时,虽然也有办法让他及时。  


我这里也只是阐述下mine和grains在通过jinja2渲染配置的场景。  我自己看来,grains更适合很久很久才变化的。 mine适合变化比较大的。 


大家show下saltstack grains的数据,几乎都是系统和硬件的信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@vm-10-154-252-82 ~ /clusterops ]$salt  '10.154.252.46'  grains.item productname
10.154.252.46:
   productname: OpenStack Nova
[root@vm-10-154-252-82 ~ /clusterops ]$salt  '10.154.252.46'  grains.item lang
10.154.252.46:
   lang: python erlang
[root@vm-10-154-252-82 ~ /clusterops ]$
[root@vm-10-154-252-82 ~ /clusterops ]$
[root@vm-10-154-252-82 ~ /clusterops ]$
[root@vm-10-154-252-82 ~ /clusterops ]$salt  '10.154.252.46'  sys.reload_modules
10.154.252.46:
     True
[root@vm-10-154-252-82 ~ /clusterops ]$


用mine的话,咱们可以定义自己感兴趣的。 可以根据现在minon使用的负载和内存情况,来分配不同的配置选项。。。。 pillar 和 mine我想大家应该能区分开来 。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@vm-10-154-252-82 ~ /clusterops ]$ time  salt  '10.154.252.46'  mine.get  '*'  status.meminfo| more
10.154.252.46:
     ----------
     10.154.252.46:
         ----------
         Active:
             ----------
             unit:
                 kB
             value:
                 506876
         Active(anon):
             ----------
             unit:
                 kB
             value:
                 94456
         Active( file ):
             ----------
             unit:
                 kB
             value:
                 412420
         AnonHugePages:
             ----------
             unit:
                 kB
             value:
                 20480
         AnonPages:
             ----------
             unit:
                 kB
             value:
                 94232
         Bounce:
             ----------



总结,一定要区分直接推送模块和mine的区别,还有grains和mine的区别。 仁者见仁,智者见智。 想怎么用,还是需要你自己掌控的。





 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1433843 ,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章