diff --git a/Dockerfile b/Dockerfile index 89340ed6d194e28da9803ebccc0f424d7e4848f1..699a269e25dfe54993fda14a9f7cbdb2339b1dee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,8 +12,6 @@ ENV GOPATH=/go WORKDIR $GOPATH RUN git clone https://github.com/OperatorFoundation/shapeshifter-dispatcher /shapeshifter-dispatcher && cd /shapeshifter-dispatcher && git reset --hard 34bd4b3fe24537a8a7a827825a1a19f2ad13adae && /usr/lib/go-1.19/bin/go build RUN strip /shapeshifter-dispatcher/shapeshifter-dispatcher -RUN /usr/lib/go-1.19/bin/go install github.com/kumina/openvpn_exporter@latest -RUN strip $GOPATH/bin/openvpn_exporter FROM registry.git.autistici.org/ai3/docker/s6-base @@ -32,10 +30,13 @@ RUN apt-get -q update \ socat \ lua-socket \ lua-sec \ + prometheus-node-exporter \ && rm -rf /var/lib/apt/lists/* +# Remove unwanted prometheus-node-exporter scripts installed by the package +RUN rm -f /var/lib/prometheus-node-exporter/* + COPY --from=build /shapeshifter-dispatcher/shapeshifter-dispatcher /usr/local/bin/shapeshifter-dispatcher -COPY --from=build /go/bin/openvpn_exporter /usr/local/bin/openvpn_exporter RUN setcap cap_net_admin,cap_net_bind_service+ep /usr/sbin/openvpn RUN setcap cap_net_admin+ep /bin/ip @@ -44,3 +45,4 @@ RUN update-alternatives --set iptables /usr/sbin/iptables-legacy # S6 configuration COPY conf/ /etc/ +COPY openvpn-metrics.sh /usr/local/sbin/openvpn-metrics.sh diff --git a/conf/services.d/openvpn-exporter/run b/conf/services.d/openvpn-exporter/run index 1349930ace7bdf728b47a2fd3d619dc670d23825..b9431aed0d921325dfb69f8354fdb239722da178 100755 --- a/conf/services.d/openvpn-exporter/run +++ b/conf/services.d/openvpn-exporter/run @@ -1,4 +1,3 @@ #!/bin/sh -echo "Starting openvpn-exporter" -exec /usr/local/bin/openvpn_exporter -openvpn.status_paths "/tmp/openvpn-status-tcp,/tmp/openvpn-status-udp" -ignore.individuals true +every 10 /usr/local/sbin/openvpn-metrics.sh diff --git a/conf/services.d/prometheus-node-exporter/finish b/conf/services.d/prometheus-node-exporter/finish new file mode 100755 index 0000000000000000000000000000000000000000..f7d228a107e06df7530641a50d87819ce5b9dbca --- /dev/null +++ b/conf/services.d/prometheus-node-exporter/finish @@ -0,0 +1,4 @@ +#!/bin/sh + +echo "Exiting prometheus-node-exporter" +s6-svscanctl -t /var/run/s6/services diff --git a/conf/services.d/prometheus-node-exporter/run b/conf/services.d/prometheus-node-exporter/run new file mode 100755 index 0000000000000000000000000000000000000000..c74f3b721b5271fa120cab1f04ed32b6d527fe38 --- /dev/null +++ b/conf/services.d/prometheus-node-exporter/run @@ -0,0 +1,4 @@ +#!/bin/sh + +echo "Starting prometheus-node-exporter" +exec /usr/bin/prometheus-node-exporter --collector.disable-defaults --collector.textfile --collector.textfile.directory /tmp/prometheus --web.listen-address=":9176" diff --git a/openvpn-metrics.sh b/openvpn-metrics.sh new file mode 100755 index 0000000000000000000000000000000000000000..82250a56b36ae3e77f26436c1eae81313b7e5e5e --- /dev/null +++ b/openvpn-metrics.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +# Execute a metrics-generating script and safely write its +# output to /tmp/prometheus +# +# +output_dir="/tmp/prometheus" +output_file="${output_dir}/openvpn.prom" +umask 022 +tmp_file="${output_file}.$$" +trap "rm -f $tmp_file 2>/dev/null" EXIT INT TERM + +mkdir -p $output_dir + +echo "# HELP openvpn_up Whether scraping OpenVPN's metrics was successful." > $tmp_file +echo '# TYPE openvpn_up gauge' >> $tmp_file +echo -n 'openvpn_up{status_path="/tmp/openvpn-status-tcp"} ' >> $tmp_file +if [ -r /tmp/openvpn-status-tcp ]; +then + echo "1" >> $tmp_file + echo "# HELP openvpn_server_connected_clients Number Of Connected Clients" >> $tmp_file + echo "# TYPE openvpn_server_connected_clients gauge" >> $tmp_file + tcp_connected=$(cat /tmp/openvpn-status-tcp |grep CLIENT_LIST|wc -l) + echo -n 'openvpn_server_connected_clients{status_path="/tmp/openvpn-status-tcp"} ' >> $tmp_file + echo $tcp_connected >> $tmp_file +else + echo "0" >> $tmp_file +fi + +echo -n 'openvpn_up{status_path="/tmp/openvpn-status-udp"} ' >> $tmp_file +if [ -r /tmp/openvpn-status-udp ]; +then + echo "1" >> $tmp_file + udp_connected=$(cat /tmp/openvpn-status-udp |grep CLIENT_LIST|wc -l) + echo -n 'openvpn_server_connected_clients{status_path="/tmp/openvpn-status-udp"} ' >> $tmp_file + echo $udp_connected >> $tmp_file +else + echo "0" >> $tmp_file +fi + +mv -f "$tmp_file" "$output_file" + +exit $?