小哥之哥 小哥之哥
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

小哥之哥

运维扫地僧
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • GO

    • GO语言基础

    • GO语言项目实战

      • Prometheus客户端Exporter开发
      • ocserv_exporter项目
        • cloud-station云商中转站
        • Prometheus监控系统对接钉钉告警通知
    • 编程
    • GO
    • GO语言项目实战
    tchua
    2023-07-04
    目录

    ocserv_exporter项目

    # 一、概述


    关于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 builder
    
    WORKDIR /apps
    
    COPY ./ /apps
    RUN export GOPROXY=https://goproxy.cn \
        && go build  -ldflags "-s -w" -o ocserv_exporter  main.go \
        && chmod +x ocserv_exporter
    
    FROM alpine
    LABEL 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.2
    RUN 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 tzdata
    
    WORKDIR /apps
    
    EXPOSE 18086
    
    CMD ["./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_exporter
    chmod +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

    编辑 (opens new window)
    #Go
    上次更新: 2023/07/13, 10:17:08
    Prometheus客户端Exporter开发
    cloud-station云商中转站

    ← Prometheus客户端Exporter开发 cloud-station云商中转站→

    最近更新
    01
    cert-manager自动签发Lets Encrypt
    09-05
    02
    Docker构建多架构镜像
    08-02
    03
    Prometheus数据迁移至VMstorage
    08-01
    更多文章>
    Theme by Vdoing | Copyright © 2023-2024 |豫ICP备2021026650号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式