栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Grafana InfluxDB 简单脚本

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Grafana InfluxDB 简单脚本

InfluxDBtools-Linux文档类资源-CSDN下载一个用于连接InfluxDB的简单工具,解压即用,密码666666密码666666密更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/zhangxueleishamo/84196848

一、需求
机器IP:ip1     ip2   ip3   
文件存放路径:/data/logs/bireport/statistics  
文件更新时间:每天凌晨1点
文件内容:第一行是超时数,第二行是总共调用数
#rsync client
1 2 * * * sh /root/rsync_bi.sh
cat /root/rsync_bi.sh
#!/bin/bash
passwdfile="/root/.rsync.passwd"
server="root@rsync.com::backup"
server_patch=$server/ip1
pathfile=/data/logs/bireport/statistics 
/usr/bin/rsync -qazu --password-file=$passwdfile $pathfile $server_patch/

#grafana server 
30 2 * * * sh /data/bireport/bi_request.sh
[root@localhost bireport]# cat bi_request.sh 
#!/bin/bash
pathdir="/data/bireport"
filename="bi_history"

ip_num=(ip1
ip2
ip3)

datetime1=`date -d "yesterday" +%Y%m%d`
datetime=`date -d "$datetime1 0 +8 hour" +"%Y%m%d %H"`
timeStamp_y=`date -d "$datetime" +%s`
#hour_8=28800000
timeStamp=`expr $timeStamp_y * 1000`

for ip in ${ip_num[@]} 
do
	if [ ! -f "$pathdir/$ip/statistics" ]; then
  		echo "ERROR: $pathdir/$ip/statistics is not exist!"
  		exit 1
	else
		timeout_num=`cat $pathdir/$ip/statistics |awk 'NR<2'`
		request_num=`cat $pathdir/$ip/statistics |awk 'NR>1'`
		rate_num=`echo  "scale=4; a = $timeout_num / $request_num; if (length(a)==scale(a)) print 0;print a"  | bc`
		echo $timeStamp $ip $request_num $timeout_num $rate_num >> $pathdir/$filename
		/usr/bin/curl -i -XPOST 'http://127.0.0.1:8086/write?db=bi_request_info&precision=ms' -u admin:cAauFWEtLerqH4EP --data-binary 'bi_info,host_id='$ip' request_num='$request_num',timeout_num='$timeout_num',rate_mun='$rate_num' '$timeStamp''
		
		mv $pathdir/$ip/statistics  $pathdir/$ip/$timeStamp"statistics"
		
	fi
done

注:因对influxdb 的时间格式不熟悉,浪费很多时间;

二、influxdb中时间格式

influxdb支持三种时间格式,epoch_time外,还支持rfc3339_date_time_string和rfc3339_like_date_time_string。

1、epoch_time格式
时间戳表示,我们一般使用的10位和13位,在influxdb中使用的时间戳是19位,单位是ns(纳秒)

2、rfc3339_date_time_string格式
这种格式为:'YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ',其中nnnnnnnnn是可选的,如果不写则会被设置为000000000。注意,如果使用这种时间格式,需要使用单引号 ’ 将时间括起来。

3、rfc3339_like_date_time_string
这种格式为:'YYYY-MM-DD HH:MM:SS.nnnnnnnnn' ,其中HH::MM:SS.nnnnnnnnn可以省略,必须用单引号包括起来。

4、按时间戳查询
秒级:
select * from disk where time >= 1542954639s and time <= 1542964713s
毫秒级:
select * from disk where time >= 1542954639000ms and time <= 1542964714000ms
纳秒级:
select * from disk where time >= 1542954639000000000ms and time <= 1542964714000000000ms

5、调整时区查询-北京时间
select * from disk where time >= '2018-11-23 14:30:39' and time <= '2018-11-23 14:32:32' tz('Asia/Shanghai')

注:
1、由于grafana要使用UTC时间,故本实例中加了8小时
2、curl 插入数据时,如插入时间 需设置时间格式 &precision=ms 为毫秒, 也可为s
3、插入的数据 --data-binary 后面需要添加单引号''

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/757139.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号