Skip to content

Commit

Permalink
新增mongodb权限配置选项
Browse files Browse the repository at this point in the history
服务器安装脚本更新
monitor api mongodb认证选项
新增监控数据管理
  • Loading branch information
guohongze committed Jun 23, 2017
1 parent b2e9e74 commit 18323c6
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 40 deletions.
7 changes: 5 additions & 2 deletions adminset.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ log_path = /var/opt/adminset/logs
log_level = info

[mongodb]
ip = 127.0.0.1
port = 27017
mongodb_ip = 127.0.0.1
mongodb_port = 27017
mongodb_user =
mongodb_pwd =
collection = sys_info


25 changes: 22 additions & 3 deletions client/adminset_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,35 @@
import requests
import platform
import socket
import psutil
import time
import schedule
import json
import threading


token = 'HPcWR7l4NJNJ'
server_ip = '192.168.47.130'

try:
import psutil
except ImportError as msg:
print(msg)
print("----------------------------------------------")
print("begining install psutil module, please waiting")
p = Popen('pip install psutil==5.2.2', stdout=PIPE, shell=True)
stdout, stderr = p.communicate()
print stdout
import psutil

try:
import schedule
except ImportError as msg:
print msg
print("------------------------------------------------")
print("begining install schedule module, please waiting")
p = Popen('pip install schedule==0.4.3', stdout=PIPE, shell=True)
stdout, stderr = p.communicate()
print stdout
import schedule


def get_ip():
hostname = socket.getfqdn(socket.gethostname())
Expand Down
37 changes: 29 additions & 8 deletions config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ 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')
mongodb_ip = config.get('mongodb', 'mongodb_ip')
mongodb_port = config.get('mongodb', 'mongodb_port')
mongodb_user = config.get('mongodb', 'mongodb_user')
mongodb_pwd = config.get('mongodb', 'mongodb_pwd')
mongodb_collection = config.get('mongodb', 'collection')
return render_to_response('config/index.html', locals(), RequestContext(request))


Expand Down Expand Up @@ -64,6 +67,9 @@ def config_save(request):
# mongodb
mongodb_ip = request.POST.get('mongodb_ip')
mongodb_port = request.POST.get('mongodb_port')
mongodb_user = request.POST.get('mongodb_user')
mongodb_pwd = request.POST.get('mongodb_pwd')
mongodb_collection = request.POST.get('mongodb_collection')

config = ConfigParser.RawConfigParser()
dirs = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -85,8 +91,11 @@ def config_save(request):
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)
config.set('mongodb', 'mongodb_ip', mongodb_ip)
config.set('mongodb', 'mongodb_port', mongodb_port)
config.set('mongodb', 'mongodb_user', mongodb_user)
config.set('mongodb', 'mongodb_pwd', mongodb_pwd)
config.set('mongodb', 'collection', mongodb_collection)
tips = u"保存成功!"
display_control = ""
with open(dirs+'/adminset.conf', 'wb') as cfgfile:
Expand All @@ -105,8 +114,11 @@ 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')
mongodb_ip = config.get('mongodb', 'mongodb_ip')
mongodb_port = config.get('mongodb', 'mongodb_port')
mongodb_user = config.get('mongodb', 'mongodb_user')
mongodb_pwd = config.get('mongodb', 'mongodb_pwd')
mongodb_collection = config.get('mongodb', 'collection')
else:
display_control = "none"
return render_to_response('config/index.html', locals(), RequestContext(request))
Expand All @@ -124,8 +136,11 @@ 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')
mongodb_ip = config.get('mongodb', 'mongodb_ip')
mongodb_port = config.get('mongodb', 'mongodb_port')
mongodb_user = config.get('mongodb', 'mongodb_user')
mongodb_pwd = config.get('mongodb', 'mongodb_pwd')
mongodb_collection = config.get('mongodb', 'collection')

if args == "a_path":
return a_path
Expand All @@ -145,6 +160,12 @@ def get_dir(args):
return mongodb_ip
if args == "mongodb_port":
return mongodb_port
if args == "mongodb_user":
return mongodb_user
if args == "mongodb_pwd":
return mongodb_pwd
if args == "mongodb_collection":
return mongodb_collection


@login_required()
Expand Down
47 changes: 47 additions & 0 deletions install/server_install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
set -e

# 初始化环境目录
main_dir="/var/opt/adminset"
adminset_dir="$main_dir/main"
data_dir="$main_dir/data"
Expand All @@ -14,13 +16,19 @@ mkdir -p $data_dir/roles
mkdir -p $config_dir
mkdir -p $logs_dir
mkdir -p $main_dir/pid

# 分发代码
rsync --delete --progress -ra --exclude '.git' $cur_dir/ $adminset_dir

# 安装依赖
echo "####install depandencies####"
yum install -y epel-release
yum install -y make autoconf automake cmake gcc gcc-c++
yum install -y python python-pip python-setuptools python-devel openssl openssl-devel
yum install -y ansible smartmontools
scp $adminset_dir/install/ansible/ansible.cfg /etc/ansible/ansible.cfg

#安装数据库
echo "####install database####"
read -p "do you want to create a new mysql database?[yes/no]:" db1
case $db1 in
Expand Down Expand Up @@ -55,6 +63,37 @@ case $db1 in
exit 1
;;
esac

# 安装mongodb
echo "####install mongodb####"
read -p "do you want to create a new Mongodb?[yes/no]:" mongo
case $mongo in
yes|y|Y|YES)
echo "installing a new Mongodb...."
yum install -y mongodb mongodb-server
/bin/systemctl start mongod
/bin/systemctl enable mongod
;;
no|n|N|NO)
read -p "your Mongodb ip address:" mongodb_ip
read -p "your Mongodb port:" mongodb_port
read -p "your Mongodb user:" mongodb_user
read -p "your Mongodb password:" mongodb_pwd
read -p "your Mongodb collection:" mongodb_collection
[ ! $mongo_password ] && echo "your db_password is empty confirm please press Enter key"
sleep 3
sed -i "s/mongodb_ip = 127.0.0.1/host = $mongo_ip/g" $adminset_dir/adminset.conf
sed -i "s/mongodb_user =/mongodb_user = $mongodb_user/g" $adminset_dir/adminset.conf
sed -i "s/mongodb_port = 27017/port = $mongodb_port/g" $adminset_dir/adminset.conf
sed -i "s/mongodb_pwd =/mongodb_pwd = $mongodb_pwd/g" $adminset_dir/adminset.conf
sed -i "s/collection = sys_info/collection = $mongodb_collection/g" $adminset_dir/adminset.conf
;;
*)
exit 1
;;
esac

# 安装主程序
echo "####install adminset####"
mkdir -p ~/.pip
cat <<EOF > ~/.pip/pip.conf
Expand All @@ -74,10 +113,14 @@ scp $adminset_dir/install/adminset.service /usr/lib/systemd/system
systemctl daemon-reload
chkconfig adminset on
service adminset start

#安装redis
echo "####install redis####"
yum install redis -y
chkconfig redis on
service redis start

# 安装celery
echo "####install celery####"
mkdir -p $config_dir/celery
scp $adminset_dir/install/celery/beat.conf $config_dir/celery/beat.conf
Expand All @@ -90,13 +133,17 @@ chkconfig celery on
chkconfig beat on
service celery start
service beat start

# 安装nginx
echo "####install nginx####"
yum install nginx -y
chkconfig nginx on
scp $adminset_dir/install/nginx/adminset.conf /etc/nginx/conf.d
scp $adminset_dir/install/nginx/nginx.conf /etc/nginx
service nginx start
nginx -s reload

# 完成安装
echo "##############install finished###################"
systemctl daemon-reload
service redis restart
Expand Down
11 changes: 9 additions & 2 deletions monitor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def received_sys_info(request):


class GetSysData(object):
collection = get_dir("mongodb_collection")

def __init__(self, hostname, monitor_item, timing, no=0):
self.hostname = hostname
Expand All @@ -39,12 +40,18 @@ def __init__(self, hostname, monitor_item, timing, no=0):
def connect_db(cls):
mongodb_ip = get_dir("mongodb_ip")
mongodb_port = get_dir("mongodb_port")
client = MongoClient(mongodb_ip, int(mongodb_port))
mongodb_user = get_dir("mongodb_user")
mongodb_pwd = get_dir("mongodb_pwd")
if mongodb_user:
uri = 'mongodb://'+mongodb_user+':'+mongodb_pwd+'@'+mongodb_ip+':'+mongodb_port+'/'+cls.collection
client = MongoClient(uri)
else:
client = MongoClient(mongodb_ip, int(mongodb_port))
return client

def get_data(self):
client = self.connect_db()
db = client.sys_info
db = client[self.collection]
collection = db[self.hostname]
now_time = int(time.time())
find_time = now_time-self.timing
Expand Down
21 changes: 21 additions & 0 deletions templates/config/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ <h4>{{ tips }}</h4>
<input type="text" value="{{ mongodb_port }}" id="mongodb_port" name="mongodb_port" style="width:450px;">
</div>
</div>
<div class="form-group">
<label for="mongodb_user" class="col-sm-2 control-label">user:</label>

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

<div class="col-sm-10">
<input type="password" value="{{ mongodb_pwd }}" id="mongodb_pwd" name="mongodb_pwd" style="width:450px;">
</div>
</div>
<div class="form-group">
<label for="mongodb_collection" class="col-sm-2 control-label">collection:</label>

<div class="col-sm-10">
<input type="text" value="{{ mongodb_collection }}" id="mongodb_collection" name="mongodb_collection" style="width:450px;">
</div>
</div>
<hr>
<p><b>Token设置</b></p>
<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<div class="pull-right hidden-xs">
</div>
<!-- Default to the left -->
<a href="http://adminset.cn" target="view_window"></a> AdminSet v0.2.0
<a href="http://adminset.cn" target="view_window"></a> AdminSet v0.3.0-alpha
</footer>
14 changes: 7 additions & 7 deletions templates/monitor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
{% if all_host %}
{% for host in all_host %}
<a href="{% url 'host_info' host.hostname 0 %}">
<div class="col-md-2">
<div class="box box-success">
{# <div class="box-header with-border">#}
{# <h3 class="box-title" >{{ host.hostname }}</h3>#}
{# </div><!-- /.box-header -->#}
<div class="box-body" style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis; width: 150px;">
{{ host.hostname }}
<div class="col-md-3" style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title" >{{ host.hostname }}</h3>
</div><!-- /.box-header -->
<div class="box-body">
{{ host.ip }}
</div><!-- /.box-body -->
</div><!-- /.box -->
</div>
Expand Down
24 changes: 7 additions & 17 deletions templates/monitor/manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,14 @@ <h3 class="box-title">监控管理</h3>
</div>
<div class="box-body pad table-responsive">
<p></p>
<table class="table table-bordered text-center">
<table class="table table-bordered">
<tr>
<th>监控数据管理</th>
<th>其它管理</th>
</tr>
<tr>
<td>
<button class="btn btn-danger" onclick="return drop_all()">
<i class="fa fa-close"></i> 清空监控数据
</button>
</td>
<td>

<div class="form-group">
<label for="range" class="col-sm-2 control-label">时间选择</label>

{# <label for="range" class="col-sm-2 control-label">时间选择</label>#}
<select id="select1" class="form-control" style="width:200px;">
<option selected="selected" value="0">7天之前</option>
<option value="1">14天之前</option>
Expand All @@ -51,13 +43,10 @@ <h3 class="box-title">监控管理</h3>
<option value="4">全部数据</option>
</select>
</div>
</td>
<td>

<div class="form-group">
<button class="btn btn-danger" onclick="return del()">
<i class="fa fa-close"></i> 删除数据
</button>
<div class="form-group">
<button class="btn btn-danger" style="float: left" onclick="return del()">
<i class="fa fa-close"></i> 删除数据
</button>
</div>
</td>
</tr>
Expand Down Expand Up @@ -95,6 +84,7 @@ <h3 class="box-title">监控管理</h3>
{#var sel = document.getElementById("select1");#}
{#var selected_test = $("#select1 option:selected").text();#}
{#var selected_value = $("#select1 option:selected").val();#}
// 获取select数据
var selected_value=0;
var selected_text="七天之前";
$(document).ready(function(){
Expand Down

0 comments on commit 18323c6

Please sign in to comment.