Skip to content

Commit

Permalink
Mongodb 可配置
Browse files Browse the repository at this point in the history
监控平台system模块信息上传接口API
agent多线程
  • Loading branch information
guohongze committed Jun 18, 2017
1 parent 059d670 commit 3112c46
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 21 deletions.
4 changes: 4 additions & 0 deletions adminset.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ token = HPcWR7l4NJNJ
log_path = /var/opt/adminset/logs
log_level = info

[mongodb]
ip = 127.0.0.1
port = 27017


18 changes: 17 additions & 1 deletion config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def index(request):
token = config.get('token', 'token')
log_path = config.get('log', 'log_path')
log_level = config.get('log', 'log_level')
mongodb_ip = config.get('mongodb', 'ip')
mongodb_port = config.get('mongodb', 'port')
return render_to_response('config/index.html', locals(), RequestContext(request))


Expand All @@ -59,7 +61,10 @@ def config_save(request):
# log
log_path = request.POST.get('log_path')
log_level = request.POST.get('log_level')

# mongodb
mongodb_ip = request.POST.get('mongodb_ip')
mongodb_port = request.POST.get('mongodb_port')

config = ConfigParser.RawConfigParser()
dirs = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
config.add_section('config')
Expand All @@ -79,6 +84,9 @@ def config_save(request):
config.add_section('log')
config.set('log', 'log_path', log_path)
config.set('log', 'log_level', log_level)
config.add_section('mongodb')
config.set('mongodb', 'ip', mongodb_ip)
config.set('mongodb', 'port', mongodb_port)
tips = u"保存成功!"
display_control = ""
with open(dirs+'/adminset.conf', 'wb') as cfgfile:
Expand All @@ -97,6 +105,8 @@ def config_save(request):
database = config.get('db', 'database')
token = config.get('token', 'token')
log_path = config.get('log', 'log_path')
mongodb_ip = config.get('mongodb', 'ip')
mongodb_port = config.get('mongodb', 'port')
else:
display_control = "none"
return render_to_response('config/index.html', locals(), RequestContext(request))
Expand All @@ -114,6 +124,8 @@ def get_dir(args):
token = config.get('token', 'token')
log_path = config.get('log', 'log_path')
log_level = config.get('log', 'log_level')
mongodb_ip = config.get('mongodb', 'ip')
mongodb_port = config.get('mongodb', 'port')

if args == "a_path":
return a_path
Expand All @@ -129,6 +141,10 @@ def get_dir(args):
return log_path
if args == "log_level":
return log_level
if args == "mongodb_ip":
return mongodb_ip
if args == "mongodb_port":
return mongodb_port


@login_required()
Expand Down
31 changes: 31 additions & 0 deletions monitor/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import json
from lib.common import token_verify
from pymongo import MongoClient
from config.views import get_dir
from django.shortcuts import HttpResponse
import time
from django.views.decorators.csrf import csrf_exempt


@csrf_exempt
def received_sys_info(request):
iToken = get_dir("token")
if request.method == 'POST':
mongodb_ip = get_dir("mongodb_ip")
mongodb_port = get_dir("mongodb_port")
received_json_data = json.loads(request.body)
# 验证token
if received_json_data["token"] != iToken:
print "forbidden your token error!!"
return HttpResponse(status=403)
hostname = received_json_data["hostname"]
received_json_data['timestamp'] = int(time.time())
client = MongoClient(mongodb_ip, int(mongodb_port))
db = client.sys_info
collection = db[hostname]
collection.insert_one(received_json_data)
return HttpResponse("Post the system Monitor Data to Server successfully!")
else:
return HttpResponse("Your push hava errors, Please Check your data!")
2 changes: 2 additions & 0 deletions monitor/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# -*- coding: utf-8 -*-
from django.conf.urls import url, include
from . import views
import api


urlpatterns = [
url(r'^system/$', views.index, name='monitor'),
url(r'^system/(?P<hostname>.+)/$', views.host_info, name='host_info'),
url(r'^get/data/(?P<hostname>.+)/$', views.get_sys_data, name='get_sys_data'),
url(r'^received/sys/info/$', api.received_sys_info, name='received_sys_info'),
]
8 changes: 6 additions & 2 deletions monitor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from django.contrib.auth.decorators import login_required
from accounts.permission import permission_verify
from pymongo import MongoClient
import json, time
import json
import time
from config.views import get_dir


@login_required()
Expand All @@ -27,10 +29,12 @@ def host_info(request, hostname):
@login_required()
@permission_verify()
def get_sys_data(request, hostname):
mongodb_ip = get_dir("mongodb_ip")
mongodb_port = get_dir("mongodb_port")
data_time = []
cpu_percent = []
memory_percent = []
client = MongoClient("127.0.0.1", 27017)
client = MongoClient(mongodb_ip, int(mongodb_port))
db = client.sys_info
collection = db[hostname]
cursor = collection.find()
Expand Down
35 changes: 19 additions & 16 deletions scripts/adminset_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import urllib
import urllib2
import requests
import platform
import socket
import psutil
Expand All @@ -14,6 +15,7 @@
import redis
import json
from pymongo import MongoClient
import threading


token = 'HPcWR7l4NJNJ'
Expand Down Expand Up @@ -114,12 +116,12 @@ def parser_disk_info(diskdata):
return pd


def post_data(data):
def post_data(url, data):
postdata = urllib.urlencode(data)
try:
req = urllib2.urlopen("http://"+server_ip+"/cmdb/collect", postdata)
req = urllib2.urlopen(url, postdata)
req.read()
print 'Post the hardwave infos to CMDB successfully!'
print 'Post the infos to adminset successfully!'
except StandardError as msg:
print msg
return True
Expand Down Expand Up @@ -150,7 +152,7 @@ def asset_info_post():
print 'Get the hardwave infos from host:'
print asset_info()
print '----------------------------------------------------------'
post_data(asset_info())
post_data("http://{}/cmdb/collect".format(server_ip), asset_info())
os.environ["LANG"] = osenv
return True

Expand Down Expand Up @@ -210,31 +212,32 @@ def agg_sys_info():
print 'Get the system infos from host:'
sys_info = dict()
sys_info['hostname'] = platform.node()
hostname = platform.node()
sys_info['timestamp'] = int(time.time())
sys_info['cpu'] = get_sys_cpu()
sys_info['mem'] = get_sys_mem()
sys_info['disk'] = get_sys_disk()
sys_info['token'] = token
print sys_info
# insert to mongodb
json_data = json.dumps(sys_info)
print '----------------------------------------------------------'
try:
client = MongoClient(server_ip, 27017)
db = client.sys_info
collection = db[hostname]
post = sys_info
collection.insert_one(post).inserted_id
print 'Post the system infos to mongodb successfully!'
except BaseException as msg:
r = requests.post("http://{}/monitor/received/sys/info/".format(server_ip), json_data)
print r.text
except StandardError as msg:
print msg
return True


def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()


if __name__ == "__main__":
asset_info_post()
time.sleep(1)
agg_sys_info()
schedule.every(1800).seconds.do(asset_info_post)
schedule.every(60).seconds.do(agg_sys_info)
schedule.every(1800).seconds.do(run_threaded, asset_info_post)
schedule.every(10).seconds.do(run_threaded, agg_sys_info)
while True:
schedule.run_pending()
time.sleep(1)
16 changes: 16 additions & 0 deletions templates/config/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ <h4>{{ tips }}</h4>
</div>
</div>
<hr>
<p><b>Mongodb设置</b></p>
<div class="form-group">
<label for="mongodb_ip" class="col-sm-2 control-label">Host:</label>

<div class="col-sm-10">
<input type="text" value="{{ mongodb_ip }}" id="mongodb_ip" name="mongodb_ip" style="width:450px;">
</div>
</div>
<div class="form-group">
<label for="mongodb_port" class="col-sm-2 control-label">Port:</label>

<div class="col-sm-10">
<input type="text" value="{{ mongodb_port }}" id="mongodb_port" name="mongodb_port" style="width:450px;">
</div>
</div>
<hr>
<p><b>Token设置</b></p>
<div class="form-group">
<label for="token" class="col-sm-2 control-label">Token:</label>
Expand Down
4 changes: 2 additions & 2 deletions templates/monitor/host_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ <h3 class="box-title">{{ hostname }} 监控信息</h3>
]
});
});
}, 2000);
}, 5000);
</script>
<script type="text/javascript">
// 基于准备好的dom初始化echarts实例
Expand Down Expand Up @@ -144,7 +144,7 @@ <h3 class="box-title">{{ hostname }} 监控信息</h3>
]
});
});
}, 2000);
}, 5000);
</script>
</section>
<!-- /.content -->
Expand Down

0 comments on commit 3112c46

Please sign in to comment.