aaahd 发表于 2018-8-1 11:33:49

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

#!/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 '''
  
<!DOCTYPE html>
  
<!-- saved from url=(0053)http://www.bootcss.com/examples/marketing-narrow.html -->
  
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
  <title>xiaorui.cc</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="">
  <meta name="author" content="">
  <!-- Le styles -->

  <link href="http://cdnjs.bootcss.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap.min.css">  <style type="text/css">
  body {
  padding-top: 20px;
  padding-bottom: 40px;
  }
  /* Custom container */
  .container-narrow {
  margin: 0 auto;
  max-width: 700px;
  }
  .container-narrow > hr {
  margin: 30px 0;
  }
  /* Main marketing message and sign up button */
  .jumbotron {
  margin: 60px 0;
  text-align: center;
  }
  .jumbotron h1 {
  font-size: 72px;
  line-height: 1;
  }
  .jumbotron .btn {
  font-size: 21px;
  padding: 14px 24px;
  }
  /* Supporting marketing content */
  .marketing {
  margin: 60px 0;
  }
  .marketing p + h4 {
  margin-top: 28px;
  }
  </style>

  <link href="http://cdnjs.bootcss.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css">  <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
  <!-->
  <script src="//cdnjs.bootcss.com/ajax/libs/html5shiv/3.6.2/html5shiv.js"></script>
  <!-->
  <!-- Fav and touch icons -->

  <link>
  <link>
  <link>
  <link>
  <link>  <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
  <body>
  <div>
  <div>
  <ul>
  <li><a href="http://www.bootcss.com/examples/marketing-narrow.html#">Home</a></li>
  <li><a href="http://www.bootcss.com/examples/marketing-narrow.html#">命令推送</a></li>
  <li><a href="http://www.bootcss.com/examples/marketing-narrow.html#">图标监控</a></li>
  </ul>
  <h3>实时监控</h3>
  </div>
  <hr>
  <div>
  <pre>%s</pre>
  <a href="http://10.10.10.66:8888">刷新</a>
  </div>
  <hr>
  <hr>
  <div>
  <h4>by xiaorui.cc</h4>
  </div>
  </div> <!-- /container -->
  <!-- Le javascript
  ================================================== -->
  <!-- Placed at the end of the document so the pages load faster -->
  
</body></html>
  
'''%p
  
@app.route('/charts.html')
  
def show_username():
  p=
  return '''
  
<!DOCTYPE html>
  
<!-- saved from url=(0053)http://www.bootcss.com/examples/marketing-narrow.html -->
  
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
  <title>xiaorui.cc</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="">
  <meta name="author" content="">

  <link href="http://67.xiaorui.cc/bootstrap.min.css">  <script type="text/javascript"src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
  <scripttype="text/javascript" src="http://67.xiaorui.cc/highcharts.js"></script>
  <scripttype="text/javascript" src="http://67.xiaorui.cc/exporting.js"></script>
  <style type="text/css">
  body {
  padding-top: 20px;
  padding-bottom: 40px;
  }
  /* Custom container */
  .container-narrow {
  margin: 0 auto;
  max-width: 700px;
  }
  .container-narrow > hr {
  margin: 30px 0;
  }
  /* Main marketing message and sign up button */
  .jumbotron {
  margin: 60px 0;
  text-align: center;
  }
  .jumbotron h1 {
  font-size: 72px;
  line-height: 1;
  }
  .jumbotron .btn {
  font-size: 21px;
  padding: 14px 24px;
  }
  /* Supporting marketing content */
  .marketing {
  margin: 60px 0;
  }
  .marketing p + h4 {
  margin-top: 28px;
  }
  </style>
  
<script type="text/javascript">
  var chart;
  $(document).ready(function() {
  chart = new Highcharts.Chart({
  chart: {
  renderTo: 'container',
  defaultSeriesType: 'line',
  marginRight: 130,
  marginBottom: 25
  },
  title: {
  text: 'netstat 监控',
  x: -20 //center
  },
  subtitle: {
  text: '让我们看看连接书哈',
  x: -20
  },
  xAxis: {
  categories: ['1', '2', '3', '4', '5', '6',
  '7', '8', '9', '10', '11', '12']
  },
  yAxis: {
  title: {
  text: '连接数'
  },
  plotLines: [{
  value: 0,
  width: 1,
  color: '#808080'
  }]
  },
  tooltip: {
  formatter: function() {
  return '<b>'+ this.series.name +'</b><br/>'+
  this.x +': '+ this.y +'°C';
  },
  },
  legend: {
  layout: 'vertical',
  align: 'right',
  verticalAlign: 'top',
  x: -10,
  y: 100,
  borderWidth: 0
  },
  series: [{
  name: '66.ruifengyun.cc',
  data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
  }, {
  name: '67.ruifengyun.cc',
  data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
  }, {
  name: 'ceshi',
  data: %s
  }]
  });
  });
  </script>
  <!-- Fav and touch icons -->

  <link>
  <link>
  <link>
  <link>
  <link>  <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
  <body>
  <div>
  <div>
  <ul>
  <li><a href="http://66.xiaorui.cc:8888">Home</a></li>
  <li><a href="http://66.xiaorui.cc:8888/pushcmd">命令推送</a></li>
  <li><a href="http://66.xiaorui.cc:8888/charts.html#">图表监控</a></li>
  </ul>
  <h3>图表监控</h3>
  </div>
  <hr>
  <div>
  <div></div>
  <a href="http://10.10.10.66:8888">刷新</a>
  </div>
  <hr>
  <hr>
  <div>
  <h4>by xiaorui.cc</h4>
  </div>
  </div> <!-- /container -->
  <!-- Le javascript
  ================================================== -->
  <!-- Placed at the end of the document so the pages load faster -->
  
</body></html>
  
'''%p
  
@app.route('/post/<int:post_id>')
  
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
  
'''
  
<form action="/login/" method="post">
  
<input type=text name=username>
  
<input type=submit value=login>
  
</form>
  
'''
  
if __name__ == "__main__":
  app.run(host="10.10.10.66",port=8888,debug=True)
页: [1]
查看完整版本: 用Saltstack的modules和grains实现实时监控平台