31
2024
01
22:24:59

ocserv_exporter项目



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

一、概述


关于ocserv 的介绍及安装这里不再赘述,可以参考文章ocserv 部署 (opens new window),这边开发关于ocserv的监控客户端,主要是为了监控该服务的用户连接使用情况。

#二、项目背景


监控指标说明:

  • ocserv 端口: 服务存活性监控

  • ocserv 客户端在线统计: vpn用户连接数统计

  • ocserv 客户端连接详情:vpn用户连接状态信息(带宽使用)

软件版本:

  • GO版本:1.20

  • ocserv 版本: 1.1.1

#三、项目介绍


源码地址ocserv_exporter(opens new window)

#3.1 Prometheus接入
  - job_name: "ocserv-exporter"
    scrape_interval: 60s
    metrics_path: '/metrics'
    static_configs:
    - targets: ['10.66.31.130:18086']
      labels:
        appname: "ocserv-exporter"
1
2
3
4
5
6
7
#3.2 告警规则

告警规则按需配置,这里主要是监控ocser存活性,以及客户端带宽使用率监控

- alert: ocserv down
    expr: sum(ocserv_status) by(instance) == 1 
    for: 5m
    labels:
      severity: critical
    annotations:
       summary: '主机{{ $labels.instance }},ocserv 连接异常!'
  - alert: ocserv client 带宽使用详情
    expr: sum(ocserv_client_info{bandwidth="receive"}) by(instance,hostname,id) / 1024 > 10
    for: 10m
    labels:
      severity: warring
    annotations:
       summary: 'vpn 主机{{ $labels.instance }},用户{{ $labels.instance }} 下行带宽使用超过 10MB/sec,已经持续10分钟,当前值: {{ printf "%.2f" $value }} MB/sec'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#3.3 配置文件
# 宿主机 相关信息# 本项目是基于ssh客户端远程调用相关命令进行指标的采集host:
  ip: "127.0.0.1" 		# ocser服务部署机器IP
  port: "22"			# ocser服务部署机器ssh端口
  passWord: "123456"	# ocser服务部署机器root密码
  sshKey: ""			# ocser服务部署机器秘钥路径# ocser服务信息app:
  port: "9143" # ocser服务端口# 是否基于Docker运行 docker: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#3.4 Dockerfile
FROM golang:1.20 as builderWORKDIR /appsCOPY ./ /appsRUN export GOPROXY=https://goproxy.cn \
    && go build  -ldflags "-s -w" -o ocserv_exporter  main.go \
    && chmod +x ocserv_exporterFROM alpineLABEL maintainer="tchua"# COPY --from=builder /apps/id_rsa  /apps/COPY --from=builder /apps/ocserv_exporter  /apps/COPY --from=builder /apps/etc/  /apps/etc/RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2RUN echo -e  "http://mirrors.aliyun.com/alpine/v3.15/main\nhttp://mirrors.aliyun.com/alpine/v3.15/community" >  /etc/apk/repositories \&& apk update && apk add tzdata nmap-ncat \&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo "Shanghai/Asia" > /etc/timezone \&& apk del tzdataWORKDIR /appsEXPOSE 18086CMD ["./ocserv_exporter"]
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

#四、项目部署


项目部署建议直接使用Docker方式,因为这里并没有去失败宿主机模式部署,而是通过远程登录的方式进行指标的采集

#4.1 构建部署
# 拉取代码https://github.com/tchuaxiaohua/ocserv_exporter.git# 启动(依赖go环境)方式一、直接启动cd ocserv_exporter
go run main.go
方式二、编译启动cd ocserv_exporter
go build -o ocserv_exporterchmod +x ocserv_exporter
./ocserv_exporter
1
2
3
4
5
6
7
8
9
10
11
12
#4.2 Docker部署推荐
# 拉去代码https://github.com/tchuaxiaohua/ocserv_exporter.git# 构建镜像cd ocserv_exporter# ## 构建执行脚本docker build -t huahua5404/ocserv-exporter:v1 .# 启动docker run -it -d -p 18086:18086 --name ocserv-exporter huahua5404/ocserv-exporter:v1
1
2
3
4
5
6
7
8
9

注意

这里如果你需要使用秘钥远程登录机器,则需要修改下Dockerfile,把COPY --from=builder /apps/id_rsa /apps/ 这行注释打开,否则会找不到秘钥文件而导致失败

#4.3 示例
  • Prometheus

image-20230704150050015

  • 指标信息

image-20230704152209974


本文链接:https://hqyman.cn/post/4973.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: