天天看点

influxdb+grafana监控网络情况

日常的服务器监控中,网络监控是必不可少的,而influxdb+grafana是我在工作中经常用到的,接下来做个部署流程记录,以下服务器基于centos7:

1.安装influxdb

下载安装包进行yum本地安装

#wget https://dl.influxdata.com/influxdb/releases/influxdb-1.2.2.x86_64.rpm  
#yum localinstall influxdb-1.2.2.x86_64.rpm
#vim /etc/influxdb/influxdb.conf
           

接下来更改influxdb的配置文件,主要是http模块,其他模块默认配置就好

[http]
  enabled = true
  bind-address = ":8086"  
  auth-enabled = false       
  log-enabled = true
  write-tracing = false
  pprof-enabled = false
  https-enabled = false
  https-certificate = "/etc/ssl/influxdb.pem"
  max-row-limit = 10000
           

2.启动influxdb,并且使用

#systemctl start influxdb 
#systemctl enable influxdb
#influx      (进入influxdb控制台)
>create database network (创建数据库)
>show databases  (列出数据库)
>drop database network (删除数据库)
>use network (进入数据库)
>create measurement ping-loss (创建ping-loss表)
>show measurements (列出所有表)
>drop measurement ping-loss (删除表)
>select * from ping-loss (查表)
>insert ping-loss, region=vn, loss=20 (插入数据,键值对形式赋值)
>select * from ping-loss where region=vn

>shouw users (查看用户)
>create user "username" with password 'password' (创建普通用户)
>create user "username" with password 'password' with all privileges (创建管理员用户)
>drop user "username" (删除用户)
>auth (用户认证,设置密码后的登录认证)
           

3.脚本监控ping丢包和延时

把监控收集的ping值入库到influxdb

#!/bin/bash

#!/bin/bash
declare -A dic

dic=([VN]="ip1" [ID]="ip2" [BR]="ip3" ...)  #字典

for key in $(echo ${!dic[*]});do
    ping -fc10 ${dic[$key]} > /tmp/ping_result/ping${key}log
    b=`cat /tmp/ping_result/ping${key}log |grep packet|awk -F "," '{print $3}'|awk -F "%" '{print $1}'|sed 's/^[ \t]*//g'`
    c=`cat /tmp/ping_result/ping${key}log  |grep rtt |awk -F',' '{print $1}' |awk -F'=' '{print $2}' |cut -d'/' -f2`
    curl -i -X POST "http://$influxdb_ip:port/write?db=network" -u test:network --data-binary "SG_${key}loss loss=$b"
    curl -i -X POST "http://$influxdb_ip:port/write?db=network"  -u test:network  --data-binary "SG_${key}laten laten=$c"
           

4.安装grafana

我就使用默认的配置启动grafana

#wget https://dl.grafana.com/oss/release/grafana-6.4.3-1.x86_64.rpm
#yum localinstall grafana-6.4.3-1.x86_64.rpm
#systemctl start grafana-server
#systemctl enable grafana-server
           

5.grafana展示出图

配置datasource

influxdb+grafana监控网络情况

配置好数据源,然后添加面板展示数据

influxdb+grafana监控网络情况

最后的结果如下

influxdb+grafana监控网络情况

最后访问grafana的web页面登录,默认用户名密码是admin,端口是3000.

继续阅读