mysql表結構mysql
自定義returnersql
fluent採集json
客戶端配置ide
服務端配置函數
背景: salt自帶的有不少可選的returner,可是都須要在minion作配置,我感受這點挺操蛋,並且正好咱們平臺上在使用fluent作採集,因而就自定義一個reutren,而後用fluent採集,處理,入庫orm
具體操做過程以下:server
mysql表結構cmd
CREATE TABLE `fluent`;同步
CREATE TABLE `salt_returns` (it
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`jid` char(20) DEFAULT NULL,
`host_id` varchar(48) DEFAULT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`fun` varchar(30) DEFAULT NULL,
`return` text,
`success` char(5) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_host_id` (`host_id`)
自定義returner
建立默認自定義return的目錄,這個目錄雖然是默認的,可是默認並無建立
# mkdir /srv/salt/_returners
自定義returner
主要就是returner(ret) 這個函數的定義
/srv/salt/_returners/lcoal_return.py
#coding=utf8
import json
def __virtual__():
return 'local_return'
def returner(ret):
'''
Return data to the local file
'''
result_file = '/var/local/salt/returner'
result = file(result_file,'a+')
result.write(str(json.dumps(ret.values()))[1:-1]+'\n')
result.close()
同步到全部節點:
salt '*' saltutil.sync_returners
執行命令
salt '*' cmd.run 'ls /var' --return local_return
查看結果:
# cat /var/log/salt/returner
"cmd.run", "20130524052158870765", "cache\ncvs\ndb\nempty\ngames\nlib\nlocal\nlock\nlog\nmail\nnis\nopt\npreserve\nrun\nspool\ntmp\nwww\nyp", "minion1", true
fluent採集
客戶端配置
<source>
type tail
path /var/log/salt/returner
pos_file /tmp/return_pos.log
tag os.salt
format /\"(?<fun>.*)\", \"(?<jid>\d+)\", (?<return>.*), \"(?<id>.*)\", (?<success>.*)/
</source>
<match os.*>
type forward
flush_interval 1s
<server>
host {{ pillar['host'] }}
port {{ pillar['port'] }}
</server>
</match>
服務端配置
<source>
type forward
port 24224
bind 0.0.0.0
</source>
<match host.os.salt>
type mysql
host localhost
database fluent
username fluent
password fluent
key_names jid,id,fun,return,success
sql INSERT INTO salt_returns (jid,host_id,fun,`return`,success) VALUES (?,?,?,?,?)
flush_interval 5s
</match>
結果查詢:
select * from salt_returns where success is not NULL and fun='cmd.run' limit 1;
+------+----------------------+-----------------------------------------+---------------------+---------+---------+---------+
| id | jid | host_id | time | fun | return | success |
+------+----------------------+-----------------------------------------+---------------------+---------+---------+---------+
| 2571 | 20130531184127393793 | test | 2013-05-31 10:38:29 | cmd.run | "/root" | true |
+------+----------------------+-----------------------------------------+---------------------+---------+---------+---------+