Код: Виділити все
#!/bin/bash
# written by Jeremie Grauer <[email protected]>
LOGDIR=/etc/openvpn/logs
# first we rotate the session file to have the last session
cat $LOGDIR/now.sessions > $LOGDIR/last.sessions
# now we create the file with the session active right now
cat $LOGDIR/status.log | sed -n '/Virtual Address/,/ROUTING/p' |grep -v "OpenVPN CLIENT LIST" |grep -v "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since" |grep -v "ROUTING TABLE" |grep -v "Virtual Address,Common Name,Real Address,Last Ref" |grep -v "GLOBAL STATS" |grep -v "Max bcast/mcast queue length,0" | grep -v "Max bcast/mcast queue length,2" |grep -v "END"|grep -v "Updated,"|cut -d',' -f1,2,5 > $LOGDIR/now.sessions
# getting the last update date
DATE=`cat $LOGDIR/status.log |sed -n 2p|cut -d',' -f2`
# we can now make a diff of the 2 files, every line beginning by "> " means that a session is in the file last.sessions but not in the now.sessions file,
# meaning that a session was terminated between the update of the 2 files
# we want an empty file to begin with
rm $LOGDIR/terminated.sessions
# we make the diff and add the current date at the end of each lines
diff $LOGDIR/now.sessions $LOGDIR/last.sessions |grep '> ' |sed -e 's/> //g'|sed -e "s/$/,$DATE/g" >> $LOGDIR/terminated.sessions
# we now read the file line by line to generate the connection duration in minute :
while read line
do
ADATE=`date -d "$(echo $line |cut -d',' -f3)" +%s`
BDATE=`date -d "$(echo $line |cut -d',' -f4)" +%s`
MINS=`echo $(echo $BDATE - $ADATE |bc) / 60 |bc`
#echo "$line"|sed -e "s/$/,Duration = $MINS mins/g" >> $LOGDIR/history.sessions
echo "$line"|sed -e "s/$/,$MINS/g" >> $LOGDIR/history.sessions
done < $LOGDIR/terminated.sessions
Код: Виділити все
chmod +x openvpn_discovery_v3.sh
Код: Виділити все
* * * * * /etc/zabbix/zabbix_agentd.d/openvpn_discovery_v3.sh
Код: Виділити все
nano /etc/zabbix/zabbix_agentd.d/userparameter_openvpn_discovery_v3.conf
Код: Виділити все
UserParameter=openvpn.active,cat /etc/openvpn/logs/now.sessions |wc -l
UserParameter=openvpn.terminated,cat /etc/openvpn/logs/terminated.sessions |wc -l
UserParameter=openvpn.whois,cat /etc/openvpn/logs/now.sessions
Так же необходимо дать разрешение пользователю zabbix просматривать логи OpenVPN сервера, для этого назначаем новые права для файла логов.
Код: Виділити все
chmod 604 /etc/openvpn/logs/status.log
Код: Виділити все
systemctl restart zabbix-agent.service
После импорта шаблона, нужно только лишь прикрепить его к нашему OpenVPN серверу и начать получать данные и графики.
Источник: https://www.itout116.ru