本文为大家分享了python+influxdb+shell写一个区域网络状况表,供大家参考,具体内容如下
shell脚本部分:
ex:就是ping 各个目的ip10个包,然后获取丢包率和平均延迟时间,在linux上设置为定时任务,每分钟执行一次。他会把数据写到influxdb服务器上面去,表示hk_vnloss和hk_vn01rtt,字段是loss和rtt,其他各区域的都是类似。
python代码部分:
1、在django项目的setting里面,配置influxdb数据库连接;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
allowed_hosts = [ '*' ] # application definition installed_apps = [ 'django.contrib.admin' , 'django.contrib.auth' , 'django.contrib.contenttypes' , 'django.contrib.sessions' , 'django.contrib.messages' , 'django.contrib.staticfiles' , 'network_ping.apps.networkpingconfig' , 'influxdb_metrics' , ] ##influxdb influxdb_host = 'influxdb服务器ip' influxdb_port = 'influxdb port' influxdb_user = 'influxdb username' influxdb_password = 'influxdb passwd' influxdb_database = 'influxdb database' influxdb_timeout = 36000 |
2、从influxdb数据库查询保存的loss和rtt数据;
1
2
3
4
5
6
7
|
from influxdb_metrics.utils import query def get_loss(tab_name): loss_res = query( 'select * from %s order by time desc limit 1' % tab_name) loss = [res for res in loss_res] return loss |
3、把数据传到web页面;
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
|
##network ping loss def network_ping(request): # vn01 ping vn01 vnloss01 = get_loss( 'vnloss' ) # vn01 ping vn02 vnloss02 = get_loss( 'vn02loss' ) # vn01 ping th thloss = get_loss( 'thloss' ) # vn01 ping id idloss = get_loss( 'idloss' ) # vn01 ping sg sgloss = get_loss( 'sgloss' ) # vn01 ping tw twloss = get_loss( 'twloss' ) # vn01 ping hk hkloss = get_loss( 'hkloss' ) # vn01 ping sh shloss = get_loss( 'shloss' ) return render(request, 'net_ping.html' , { 'vn01' : { 'vnloss01' : vnloss01, 'vnloss02' : vnloss02, 'thloss' : thloss, 'hkloss' : hkloss, 'idloss' : idloss, 'sgloss' : sgloss, 'twloss' : twloss, 'shloss' : shloss },...) |
4、在web页面获取进行展示;
1
2
3
4
5
6
7
8
9
10
11
|
<tr> <td bgcolor = "#f0f8ff" class = "titlestyle" >vn - 01 <br / > 45.119 . 241.249 < / td> <td tloss = {{ vn01.vnloss01. 0.0 .loss }}>< / td> <td tloss = {{ vn01.vnloss02. 0.0 .loss }}>< / td> <td tloss = {{ vn01.thloss. 0.0 .loss }}>< / td> <td tloss = {{ vn01.idloss. 0.0 .loss }}>< / td> <td tloss = {{ vn01.sgloss. 0.0 .loss }}>< / td> <td tloss = {{ vn01.twloss. 0.0 .loss }}>< / td> <td tloss = {{ vn01.hkloss. 0.0 .loss }}>< / td> <td tloss = {{ vn01.shloss. 0.0 .loss }}>< / td> < / tr> |
5、js定时刷新页面;
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$(function () { $( "[tloss]" ).each(function () { var loss_res = $(this).attr( 'tloss' ); if (loss_res < 10 ) { $(this).html( '<span class="greentyle"></span><br/>loss: <span bg>' + loss_res); } else if (loss_res < = 20 ) { $(this).html( '<span class="yellowstyle"></span><br/>loss: ' + loss_res); } else { $(this).html( '<span class="redstyle"></span><br/>loss: ' + loss_res); } }); settimeout( "self.location.reload();" , 60000 ); }) |
6、最后的效果展示;
最后:页面是每分钟定时刷新一次,但这个都是当前ping的loss和rtt值,要想看历史的ping情况,可以搭建一个grafana,然后grafana配置数据源为influxdb,就可以查看历史曲线等等,例如:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/linxi7/article/details/78276363