Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nodata #5

Merged
merged 1 commit into from
Oct 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def redirect_to_sso():
return resp


from web.controller import home, group, plugin, host, expression, api, template, strategy
from web.controller import home, group, plugin, host, expression, api, template, strategy, nodata
81 changes: 81 additions & 0 deletions web/controller/nodata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding:utf-8 -*-
__author__ = 'niean'
from web import app
from flask import request, g, render_template, jsonify
from web.model.nodata import Nodata
from frame.params import required_chk
from frame.config import UIC_ADDRESS


@app.route('/nodatas')
def nodatas_get():
g.menu = 'nodatas'
page = int(request.args.get('p', 1))
limit = int(request.args.get('limit', 5))
query = request.args.get('q', '').strip()
mine = request.args.get('mine', '1')
me = g.user_name if mine == '1' else None
vs, total = Nodata.query(page, limit, query, me)
return render_template(
'nodata/list.html',
data={
'vs': vs,
'total': total,
'query': query,
'limit': limit,
'page': page,
'mine': mine,
}
)

@app.route('/nodata/add')
def nodata_add_get():
g.menu = 'nodatas'
o = Nodata.get(int(request.args.get('id', '0').strip()))
return render_template('nodata/add.html',
data={'nodata': o, 'uic_address': UIC_ADDRESS['external']})


@app.route('/nodata/update', methods=['POST'])
def nodata_update_post():
nodata_id = request.form['nodata_id'].strip()
name = request.form['name'].strip()
obj = request.form['obj'].strip()
obj_type = request.form['obj_type'].strip()
metric = request.form['metric'].strip()
tags = request.form['tags'].strip()
dstype = request.form['dstype'].strip()
step = request.form['step'].strip()
mock = request.form['mock'].strip()

msg = required_chk({
'name' : name,
'endpoint' : obj,
'endpoint_type' : obj_type,
'metric' : metric,
'type' : dstype,
'step' : step,
'mock_value': mock,
})

if msg:
return jsonify(msg=msg)

return jsonify(msg=Nodata.save_or_update(
nodata_id,
name,
obj,
obj_type,
metric,
tags,
dstype,
step,
mock,
g.user_name,
))

@app.route('/nodata/delete/<nodata_id>')
def nodata_delete_get(nodata_id):
nodata_id = int(nodata_id)
Nodata.delete_one(nodata_id)
return jsonify(msg='')
111 changes: 111 additions & 0 deletions web/model/nodata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# -*- coding:utf-8 -*-
__author__ = 'Ulric Qin'
from .bean import Bean
from frame.config import MAINTAINERS
from frame.api import uic
import time


class Nodata(Bean):
_tbl = 'mockcfg'
_cols = 'id, name, obj, obj_type, metric, tags, dstype, step, mock, creator, t_create, t_modify'
_max_obj_items = 5
_max_obj_len = 1024

def __init__(self, _id, name, obj, obj_type, metric, tags, dstype, step, mock, creator,
t_create, t_modify):
self.id = _id
self.name = name
self.obj = obj
self.obj_type = obj_type
self.metric = metric
self.tags = tags
self.dstype = dstype
self.step = step
self.mock = mock
self.creator = creator
self.t_create = t_create
self.t_modify = t_modify

@classmethod
def query(cls, page, limit, query, me=None):
where = ''
params = []

if me is not None:
where = 'creator = %s'
params.append(me)

if query:
where += ' and ' if where else ''
where += ' name like %s'
params.append('%' + query + '%')

vs = cls.select_vs(where=where, params=params, page=page, limit=limit)
total = cls.total(where=where, params=params)
return vs, total

@classmethod
def save_or_update(cls, nodata_id, name, obj, obj_type, metric, tags, dstype, step, mock, login_user):
if len(obj) > cls._max_obj_len:
return 'endpoint too long'

obj_len = len(obj.strip().split('\n'))
if obj_len > cls._max_obj_items:
return 'endpoint too many items'

if nodata_id:
return cls.update_nodata(nodata_id, name, obj, obj_type, metric, tags, dstype, step, mock)
else:
return cls.insert_nodata(name, obj, obj_type, metric, tags, dstype, step, mock, login_user)

@classmethod
def insert_nodata(cls, name, obj, obj_type, metric, tags, dstype, step, mock, login_user):
nodata_id = Nodata.insert({
'name' : name,
'obj' : obj,
'obj_type' : obj_type,
'metric' : metric,
'tags' : tags,
'dstype' : dstype,
'step' : step,
'mock': mock,
'creator': login_user,
't_create': int(time.time())
})

if nodata_id:
return ''

return 'save nodata fail'

@classmethod
def update_nodata(cls, nodata_id, name, obj, obj_type, metric, tags, dstype, step, mock):
e = Nodata.get(nodata_id)
if not e:
return 'no such nodata config %s' % nodata_id

Nodata.update_dict(
{
'name' : name,
'obj' : obj,
'obj_type' : obj_type,
'metric' : metric,
'tags' : tags,
'dstype' : dstype,
'step' : step,
'mock': mock,
},
'id=%s',
[e.id]
)
return ''

def writable(self, login_user):
if self.creator == login_user:
return True

if login_user in MAINTAINERS:
return True

return False
1 change: 1 addition & 0 deletions web/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ <h1 style="font-weight: bold; font-family: Georgia,Times,'Times New Roman',serif
<li role="presentation" class="{% if not g.menu %}active{% endif %}"><a href="/">HostGroups</a></li>
<li role="presentation" class="{% if g.menu == 'templates' %}active{% endif %}"><a href="{{ url_for('templates_get') }}">Templates</a></li>
<li role="presentation" class="{% if g.menu == 'expressions' %}active{% endif %}"><a href="{{ url_for('expressions_get') }}">Expressions</a></li>
<li role="presentation" class="{% if g.menu == 'nodatas' %}active{% endif %}"><a href="{{ url_for('nodatas_get') }}">Nodatas</a></li>
</ul>
</div>

Expand Down
124 changes: 124 additions & 0 deletions web/templates/nodata/add.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{% extends "layout.html" %}
{% block content %}
<link rel="stylesheet" href="{{ url_for('static', filename='select2/select2.css') }}"/>
<link rel="stylesheet" href="{{ url_for('static', filename='css/select2-bootstrap.css') }}"/>
<script type="text/javascript" src="{{ url_for('static', filename='select2/select2.min.js') }}"></script>

<script>
function query_nodata() {
var query = $.trim($("#query").val());
var mine = document.getElementById('mine').checked ? 1 : 0;
window.location.href = '/nodatas?q=' + query + '&mine=' + mine;
}
function update_nodata() {
$.post(
'/nodata/update',
{
'name': $.trim($("#name").val()),
'obj_type': $.trim($("#obj_type").val()),
'obj': $.trim($("#obj").val()),
'metric': $.trim($("#metric").val()),
'tags': $.trim($("#tags").val()),
'dstype': $.trim($("#dstype").val()),
'step': $.trim($("#step").val()),
'mock': $.trim($("#mock").val()),
'nodata_id': $.trim($("#nodata_id").val())
},
function (json) {
handle_quietly(json);
}
);
}
function on_endpoint_changed(){
var d = $("#obj_type").val()
if (d == "group"){
$("#obj").attr('placeholder', "cop.xiaomi_owt.inf_pdl.falcon")
} else if(d == "host") {
$("#obj").attr('placeholder', "c3-op-mon-nodata01.bj\nc3-op-mon-nodata02.bj")
} else {
$("#obj").attr('placeholder', "11.11.0.11:9900\n11.11.0.12:9900")
}
}
</script>


<input type="hidden" id="nodata_id" value="{{ data.nodata.id }}">

<div style="padding: 20px;">
<div class="panel panel-default">
<div class="panel-heading">{% if data.nodata.id %}modify{% else %}add{% endif %} nodata</div>
<div class="panel-body">
<div role="form">

<div class="form-group">
<label for="name">name:</label>
<input type="text" placeholder="nodata.inf.falcon" value="{{ data.nodata.name }}" class="form-control" id="name">
</div>

<div class="form-group">
<label for="obj_type">
endpoint选择:
</label>
<a target="_blank" href="http://wiki.n.miui.com/pages/viewpage.action?pageId=12785497#Nodata使用手册-注意事项" title="endpoint配置说明">
<span class="glyphicon glyphicon-question-sign" style="float:right;"></span>
</a>
<select class="form-control" id="obj_type" style="vertical-align:top" onchange="on_endpoint_changed()">
<option value="group" {% if data.nodata.obj_type == 'group' %}selected{% endif %}>tag串</option>
<option value="host" {% if data.nodata.obj_type == 'host' %}selected{% endif %}>机器名</option>
<option value="other" {% if data.nodata.obj_type == 'other' %}selected{% endif %}>其他</option>
</select>
<textarea class="form-control" rows="3" id="obj" placeholder="cop.xiaomi_owt.inf_pdl.falcon" style="margin-top: 5px;">{{ data.nodata.obj }}</textarea>
</div>

<div class="form-group">
<label for="metric">metric:</label>
<input type="text" placeholder="agent.alive" value="{{ data.nodata.metric }}" class="form-control" id="metric">
<span class="help-block"></span>
</div>

<div class="form-group">
<label for="tags">tags:</label>
<input type="text" {% if not data.nodata.id %}placeholder="pdl=falcon,module=nodata"{% endif %} value="{{ data.nodata.tags }}" class="form-control" id="tags">
</div>

<div class="form-group">
<label for="dstype">type:</label>
<select class="form-control" id="dstype">
<option value="GAUGE" {% if data.nodata.dstype == 'GAUGE' %}selected{% endif %}>GAUGE</option>
</select>
</div>

<div class="form-group">
<label for="step">周期(秒):</label>
<input type="text" placeholder="60" value="{{ data.nodata.step }}" class="form-control" id="step">
</div>

<div style="border-left: 10px solid green; padding-left: 5px;margin-bottom: 10px;">
<span>数据上报中断时, 补发如下值:</span>
</div>
<div class="form-group">
<input type="text" placeholder="-1" value="{{ data.nodata.mock}}" class="form-control" id="mock">
</div>

<div class="mb20">
<button class="btn btn-primary" onclick="update_nodata();">
<span class="glyphicon glyphicon-floppy-disk"></span>
Submit
</button>
<a href="{{ url_for('nodatas_get') }}" class="btn btn-default">
<span class="glyphicon glyphicon-arrow-left"></span>
Back
</a>
</div>

</div>
</div>
</div>

<a class="orange" target="_blank" href="http://book.open-falcon.com">
<span class="glyphicon glyphicon-question-sign"></span>
Nodata监控用户手册
</a>
</div>

{% endblock %}
Loading