# 用Saltstack的modules和grains实现实时监控平台

+关注继续查看

1.  通过saltstack modules 和grains 收集信息

2.  然后放到页面中

 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 #!/usr/bin/env python #coding=utf-8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             from flask import Flask,url_for,request,render_template,redirect,abort,escape,session from werkzeug import secure_filename import os                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             print __name__ app = Flask(__name__) app.secret_key = 'hello'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             @app.route('/') def index():     p=os.popen('salt \'*\' grains.item psnum netnum').read()     p=os.popen('salt \* cmd.run \'netstat -an|wc -l\'').read()     return '''             xiaorui.cc                                                                                                                                                                  Home           
• 命令推送
•            
• 图标监控
•                   实时监控              
               
%s
        刷新              
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
               

by xiaorui.cc

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     '''%p                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             @app.route('/user/') def show_username(username):     return username                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             @app.route('/post/') def show_post(post_id):     return 'post_id:%d' % post_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         @app.route('/index/') def test_session():     if 'username' in session:         return 'logged in as %s' % escape(session['username'])     return redirect(url_for('login'))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             @app.route('/login/',methods=['GET','POST']) def login():     if request.method == 'POST':         session['username'] = request.form['username']         return redirect(url_for('test_session'))     else:         return '''                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ''' @app.route('/setcookie') def set_cookie():     if 'num' in request.cookies:         count = int(request.cookies['num']) + 1     else:         count = 0     response = app.make_response(str(count))     response.set_cookie('num',value=count,max_age=None,expires=None,domain=None)     return response if __name__ == "__main__":     app.run(host="10.10.10.66",port=8888,debug=True)

xiaorui.cc

花了半小时把图表展现给更新出来，这个只是简单的demo。

让大家体验下 数据填充图表的做法。

后端数据都是静态的，下次再写个动态的渲染。

关于这些业务数据，大家可以把执行结果往库里仍，然后从库里拉出来。可以用saltstack的returners把数据搞到mysql或者mongodb。

或者是放一个全局的变量，把数据往list的最后面放，满12个数值话，开始清除第一个，然后把数据往后放。。。。

或者是把saltstack的执行结果放到文本里面，两个列  一个是时间轴 一个是数据轴。然后搞出来，渲染到前端。

总之，能搞出规律的x轴和y轴就行啦。。。

 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 #!/usr/bin/env python #coding=utf-8 #xiaorui.cc from flask import Flask,url_for,request,render_template,redirect,abort,escape,session from werkzeug import secure_filename import os                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         print __name__ app = Flask(__name__) app.secret_key = 'hello'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         @app.route('/') def index():     p=os.popen('salt \'*\' grains.item psnum netnum').read()     p=os.popen('salt \* cmd.run \'netstat -an|wc -l\'').read()     return '''             xiaorui.cc                                                                                                                                                                  Home           
• 命令推送
•            
• 图标监控
•                   实时监控              
               
%s
        刷新              
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
               

by xiaorui.cc

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 '''%p                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         @app.route('/charts.html') def show_username():     p=[1,2,3,4,5,6,7,8,9,1,2,3]     return '''             xiaorui.cc                                                                                                                                                                    
• Home
•            
• 命令推送
•            图表监控                  图表监控              
               
        刷新              
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
               

by xiaorui.cc

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 '''%p                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         @app.route('/post/') def show_post(post_id):     return 'post_id:%d' % post_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 @app.route('/index/') def test_session():     if 'username' in session:         return 'logged in as %s' % escape(session['username'])     return redirect(url_for('login'))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         @app.route('/login/',methods=['GET','POST']) def login():     if request.method == 'POST':         session['username'] = request.form['username']         return redirect(url_for('test_session'))     else:         return '''                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ''' if __name__ == "__main__":     app.run(host="10.10.10.66",port=8888,debug=True)

还有一种展现方式，就是油表

这个数据很方面的传到页面上去的。

通过后端改变value的值就行啦~

 1 2 3 4 5 6 7 8    要是不显示的话，再加上这段语句 \$(".dial").knob({ 'min':-40 ,'max':70 })

这里说下 saltstack  grains 的采集~ 例子如下

我要可以取出 name和lang的值。    [xiaorui.cc]

采集全部信息

 1 salt '*' grains.items

我们通过items查看 很多的系统信息。

实时采集的话，就要取消master对grains的缓存

更新数据缓存，结果

貌似刷新有点问题~   应该是他的一个bug吧~

经过 saltstack 专业人士  大牛  大神 帅锅 沈灿 的一段时间折腾得知，grains信息是每次客户端启动后 就确定了的   除非你每次采集钱都 重启 minion 或者 重新同步下 grains。

页面上又增加了几个功能，其实睡觉前写完了，但是有些bug

等都调试好了后，在给大家贴下实现代码。

多选控件                          【xiaorui.cc】

模块上传

编辑各种              【xiaorui.cc】

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

版权声明：本文内容由阿里云实名注册用户自发贡献，版权归原作者所有，阿里云开发者社区不拥有其著作权，亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容，填写侵权投诉表单进行举报，一经查实，本社区将立刻删除涉嫌侵权内容。

作者高分内容
更多
相关文章
+关注
文章
问答
作者高分内容
更多
文章排行榜
最热
最新
推荐文章
相关电子书
更多
ES6 必知必会
立即下载
低代码开发师（初级）实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载