一、概述
接前文 网络流量监控神器vnStat初探 我们已经了解了vnStat的作用、使用和docker部署。
同时也了解到官方版本支持的采样统计间隔最小为5分钟级
,现在我们有一个需求希望将采样统计间隔修改为分钟级
,我们该怎么做呢?
二、官网参考
1. 官网地址
点击 vnstat-docker 进入官网,我们看到:
下载Dockerfile
文件,该文件定义了镜像打包过程。
2. 查看打包过程
Dockerfile
FROM alpine:latest AS base
LABEL org.opencontainers.image.authors="Teemu Toivola"
LABEL org.opencontainers.image.url="https://humdi.net/vnstat/"
LABEL org.opencontainers.image.source="https://github.com/vergoh/vnstat-docker"
LABEL org.opencontainers.image.title="vnStat"
LABEL org.opencontainers.image.description="vnStat (https://humdi.net/vnstat/) in a container with image output via http"
ENV HTTP_PORT=8685
ENV HTTP_BIND=*
ENV HTTP_LOG=/dev/stdout
ENV LARGE_FONTS=0
ENV CACHE_TIME=1
ENV RATE_UNIT=1
ENV INTERFACE_ORDER=0
ENV QUERY_MODE=0
ENV DARK_MODE=0
ENV PAGE_REFRESH=0
ENV RUN_VNSTATD=1
COPY favicon.ico /var/www/localhost/htdocs/favicon.ico
COPY start.sh /
RUN true \
&& set -ex \
&& apk add --no-cache \
tzdata \
gd \
perl \
lighttpd \
sqlite-libs \
curl
FROM alpine:latest AS builder
RUN true \
&& set -ex \
&& apk add --no-cache \
gcc \
make \
musl-dev \
linux-headers \
gd-dev \
sqlite-dev \
&& wget https://humdi.net/vnstat/vnstat-latest.tar.gz \
&& tar zxvf vnstat-latest.tar.gz \
&& cd vnstat-*/ \
&& ./configure --prefix=/usr --sysconfdir=/etc \
&& make \
&& make install
FROM base AS runtime
COPY --from=builder /usr/bin/vnstat /usr/bin/vnstat
COPY --from=builder /usr/bin/vnstati /usr/bin/vnstati
COPY --from=builder /usr/sbin/vnstatd /usr/sbin/vnstatd
COPY --from=builder /etc/vnstat.conf /etc/vnstat.conf
COPY --from=builder vnstat-*/examples/vnstat.cgi /var/www/localhost/htdocs/index.cgi
COPY --from=builder vnstat-*/examples/vnstat-json.cgi /var/www/localhost/htdocs/json.cgi
COPY --from=builder vnstat-*/examples/vnstat-metrics.cgi /var/www/localhost/htdocs/metrics.cgi
RUN true \
&& set -ex \
&& addgroup -S vnstat \
&& adduser -S -h /var/lib/vnstat -s /sbin/nologin -g vnStat -D -H -G vnstat vnstat
VOLUME /var/lib/vnstat
EXPOSE ${HTTP_PORT}
CMD [ "/start.sh" ]
阅读此文件,注意到
此部分脚本实现了https://humdi.net/vnstat/vnstat-latest.tar.gz 源码下载与解压
。
我们提前下载源码,并修改脚本为:
COPY vnstat-latest.tar.gz \
&& tar zxvf vnstat-latest.tar.gz \
此代码与上面的管方代码功能基本等价,区别是我们需要提前将源码下载到本地
为啥这么做呢?
当然是为了方便我们在官方源码的基础上做修改
3.打包命令
在Dockerfile 文件同级目录新建build.sh文件,内容如下:
#!/bin/sh
docker build -t vergoh/vnstat:test .
执行下面的命令打包镜像
sh build.sh
三、修改过的文件
修改过程就不详细说明了,这边把修改结果文件放出来,方便有兴趣的同学研究! 阅读源码需要有一点C语言基础!
注意: vnstat-latest.bak.tar.gz 为官方原始文件,vnstat-latest.tar.gz为修改后的文件
https://gitcode.com/00fly/vnstat-docker/overview
四、部署运行
1. 编排文件
docker-compose.yml
version: "3.7"
services:
vnstat-data:
image: vergoh/vnstat:test
container_name: vnstat
deploy:
resources:
limits:
cpus: '1.0'
memory: 10M
reservations:
cpus: '0.80'
memory: 6M
restart: unless-stopped
network_mode: "host"
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- ./vnstatdb:/var/lib/vnstat
environment:
- HTTP_PORT=0
- EXCLUDE_PATTERN=^docker|^veth|^br-
logging:
driver: 'json-file'
options:
max-size: '5m'
max-file: '1'
vnstat-web:
image: vergoh/vnstat:test
container_name: vnstat-web
deploy:
resources:
limits:
cpus: '1.0'
memory: 20M
reservations:
cpus: '0.80'
memory: 10M
restart: unless-stopped
ports:
- 8086:8685
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- ./vnstatdb:/var/lib/vnstat:ro
environment:
- HTTP_PORT=8685
- HTTP_LOG=/dev/stdout
- LARGE_FONTS=0
- CACHE_TIME=1
- RATE_UNIT=1
- INTERFACE_ORDER=0
- QUERY_MODE=0
- DARK_MODE=0
- PAGE_REFRESH=30
- RUN_VNSTATD=0
logging:
driver: 'json-file'
options:
max-size: '5m'
max-file: '1'
2. 运行效果
http://124.71.129.204:8086/?s-0-5
有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!
-over-