diff --git a/scripts/batman-gateway-mode-enabled.sh b/scripts/batman-gateway-mode-enabled.sh new file mode 100755 index 0000000000000000000000000000000000000000..3089e8ec9e4359d14d80a5bf822cad83f53fed47 --- /dev/null +++ b/scripts/batman-gateway-mode-enabled.sh @@ -0,0 +1,2 @@ +#!/bin/sh +/usr/local/sbin/batctl gw | grep server | wc -l diff --git a/scripts/batman-maximum-gateway-metric.sh b/scripts/batman-maximum-gateway-metric.sh new file mode 100755 index 0000000000000000000000000000000000000000..518d2992c52106b774da433ef0766d9d781771d3 --- /dev/null +++ b/scripts/batman-maximum-gateway-metric.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +GATEWAYS=$(/usr/local/sbin/batctl gwl | awk -F' ' 'NR <= 1 {next} {print $1}') +METRIC=0 + +for i in $GATEWAYS +do + TRACEROUTE=$(/usr/local/sbin/batctl traceroute "$i" | awk 'NR>1') + if [[ "$TRACEROUTE" =~ "Unreachable" ]]; then + echo 99 + exit + fi + + THISMETRIC=$(wc -l <<< $TRACEROUTE) + + if [ "$THISMETRIC" -gt "$METRIC" ]; then + METRIC=$THISMETRIC + fi +done + +echo $METRIC diff --git a/scripts/batman-visible-gateway-count.sh b/scripts/batman-visible-gateway-count.sh new file mode 100755 index 0000000000000000000000000000000000000000..f760684ab6c7ed42a70bdc0ca35a52ecb8b7dde6 --- /dev/null +++ b/scripts/batman-visible-gateway-count.sh @@ -0,0 +1,2 @@ +#!/bin/sh +expr $(/usr/local/sbin/batctl gwl | wc -l) - 1 diff --git a/scripts/batman_originators.sh b/scripts/batman_originators.sh new file mode 100755 index 0000000000000000000000000000000000000000..744ba445a66293df2a3c3e90888943a5004789a2 --- /dev/null +++ b/scripts/batman_originators.sh @@ -0,0 +1,2 @@ +#!/bin/bash +batctl tg | cut -d")" -f2 | cut -d" " -f3 | grep -v "^$" | sort | uniq | wc -l diff --git a/scripts/compile_to_radvd_adv_count.cpp b/scripts/compile_to_radvd_adv_count.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7af03b8e5bb0ebfac8b4710d3baa66cfdcc4e4d7 --- /dev/null +++ b/scripts/compile_to_radvd_adv_count.cpp @@ -0,0 +1,196 @@ +#include <unistd.h> +#include <iostream> +#include <sstream> +#include <string> +#include <time.h> +#include <stdint.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <errno.h> +#include <cstdio> +#include <map> +#include <list> + +#define MAX_GW_AGE 300 +#define SOCKET "/tmp/radvdump_gateway_count.sock" +int stop = 0; +int radvdFD = 0; +std::map<std::string, time_t> routerLastSeenMap; + +void runRadvd() { + if (radvdFD != 0) { + close(radvdFD); + } + + int fd[2]; + pipe(fd); + + int pid = fork(); + if (pid == 0) { + close(fd[0]); + dup2(fd[1], 1); + execlp("radvdump", "radvdump", NULL); + } else if (pid < 0) { + // error + } else { + close(fd[1]); + radvdFD = fd[0]; + } +} + +int openConnection() { + int s, t, len; + struct sockaddr_un remote; + char str[100]; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + return -1; + } + remote.sun_family = AF_UNIX; + strcpy(remote.sun_path, SOCKET); + len = strlen(remote.sun_path) + sizeof(remote.sun_family); + + if (connect(s, (struct sockaddr *)&remote, len) == -1) { + return -1; + } else { + return s; + } +} + +void updateRouter(const std::string &routerString) { + if (routerLastSeenMap.find(routerString) != routerLastSeenMap.end()) { + routerLastSeenMap.erase(routerString); + } + + routerLastSeenMap.insert(std::make_pair(routerString, time(NULL))); +} + +void parseBlock(const std::string &string) { + std::stringstream ss(string); + std::string line; + + while (std::getline(ss, line, '\n')) { + size_t pos; + std::string needle("based on Router Advertisement from"); + if ((pos = line.find(needle)) != std::string::npos) { + std::string routerString = line.substr(pos + needle.size() + 1); + updateRouter(routerString); + } + } +} + +uint64_t countRouters() { + std::list<std::string> timedOut; + uint64_t count = 0; + time_t now = time(NULL); + for (std::map<std::string, time_t>::iterator it = routerLastSeenMap.begin(); it != routerLastSeenMap.end(); ++it) { + if (now - it->second < MAX_GW_AGE) { + count++; + } else { + timedOut.push_back(it->first); + } + } + + for (std::list<std::string>::iterator it = timedOut.begin(); it != timedOut.end(); ++it) { + routerLastSeenMap.erase(*it); + } + return count; +} + +int createSocket() { + int create_socket; + if((create_socket = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) { + perror("socket"); + return -1; + } + + unlink(SOCKET); + + struct sockaddr_un address; + socklen_t addrlen; + address.sun_family = AF_UNIX; + strcpy(address.sun_path, SOCKET); + addrlen = sizeof(address); + + if(bind(create_socket, (struct sockaddr *)&address, addrlen) != 0) { + perror("bind"); + return -1; + } + + listen(create_socket, 5); + chmod(SOCKET, 0666); + + return create_socket; +} + +void handleClient(int socket) { + struct sockaddr_un address; + socklen_t addrlen; + int client = accept(socket, (struct sockaddr *)&address, &addrlen); + if (client >= 0) { + std::stringstream ss(""); + ss << countRouters() << "\n"; + write(client, ss.str().c_str(), ss.str().size()); + close(client); + } +} + + +void serverMode() { + int socket = createSocket(); + runRadvd(); + while (!stop) { + static fd_set fdSet; + FD_ZERO(&fdSet); + //FD_SET(0, &fdSet); + FD_SET(socket, &fdSet); + FD_SET(radvdFD, &fdSet); + + int maxFd = radvdFD; + + int res = select(maxFd + 1, &fdSet, NULL, NULL, NULL); + if (res == -1) + continue; + + if (FD_ISSET(socket, &fdSet)) { + handleClient(socket); + } + + if (FD_ISSET(radvdFD, &fdSet)) { + char buf[1024]; + int n = read(radvdFD, buf, 1024); + if (n > 0) { + parseBlock(std::string(buf, n)); + } + } + + if (FD_ISSET(0, &fdSet)) { + char buf[16]; + int n = read(0, buf, 16); + std::cout << countRouters() << std::endl; + } + } +} + +int main(void) { + + int clientFd = openConnection(); + if (clientFd == -1) { + int pid = fork(); + if (pid == 0) + serverMode(); + sleep(10); + clientFd = openConnection(); + if (clientFd == -1) { + std::cout << "FAIL." << std::endl; + } + } + + char buf[128]; + size_t n = read(clientFd, buf, 128); + write(1, buf, n); + + return 0; +} diff --git a/scripts/conntrack_count.sh b/scripts/conntrack_count.sh new file mode 100755 index 0000000000000000000000000000000000000000..65757ba2414c8ce3cdc8ea4748ec7cf5661ed896 --- /dev/null +++ b/scripts/conntrack_count.sh @@ -0,0 +1,2 @@ +#!/bin/sh +cat /proc/sys/net/ipv4/netfilter/ip_conntrack_count diff --git a/scripts/conntrack_max.sh b/scripts/conntrack_max.sh new file mode 100755 index 0000000000000000000000000000000000000000..c2d515aa2356aa37bfdcf34ddccde9cc382475ba --- /dev/null +++ b/scripts/conntrack_max.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cat /proc/sys/net/ipv4/netfilter/ip_conntrack_max + diff --git a/scripts/dhcp_server_running.sh b/scripts/dhcp_server_running.sh new file mode 100755 index 0000000000000000000000000000000000000000..2aa25cb91afc9d32915ad18c9b3931beace57bcb --- /dev/null +++ b/scripts/dhcp_server_running.sh @@ -0,0 +1,2 @@ +#!/bin/sh +ps ax | grep dhcpd | grep -v grep | wc -l diff --git a/scripts/dhcppools.sh b/scripts/dhcppools.sh new file mode 100755 index 0000000000000000000000000000000000000000..2202919ff02a3065f28a22dbcdf82a24f7af31ff --- /dev/null +++ b/scripts/dhcppools.sh @@ -0,0 +1,16 @@ +#!/bin/bash +pools_all=`dhcpd-pools -c /etc/dhcp/dhcpd.conf -l /var/lib/dhcp/dhcpd.leases -L02|egrep -io "^[a-z]*(\-|\_)?[a-z]*"` +pools_qtd=`echo $pools_all|wc -w` +retorno=`echo -e "{\n\t\t"\"data\"":["` +for p in $pools_all +do + if [ "$pools_qtd" -le "1" ] + then + retorno=$retorno`echo -e "\n\t\t{\"{#DHCPPOOL}\":\"$p\"}"` + else + retorno=$retorno`echo -e "\n\t\t{\"{#DHCPPOOL}\":\"$p\"},"` + fi + pools_qtd=$(($pools_qtd - 1)) +done +retorno=$retorno`echo -e "]}"` +echo -e $retorno diff --git a/scripts/radvd_adv_count b/scripts/radvd_adv_count new file mode 100755 index 0000000000000000000000000000000000000000..5eb8812e08bfa10a194e0fbd0b41aa9a2594c123 Binary files /dev/null and b/scripts/radvd_adv_count differ diff --git a/scripts/route-table42-reachable.sh b/scripts/route-table42-reachable.sh new file mode 100755 index 0000000000000000000000000000000000000000..44b757ce4a3e3998ce3067f852f0292d5f6930da --- /dev/null +++ b/scripts/route-table42-reachable.sh @@ -0,0 +1,14 @@ +#!/bin/sh +TARGET=8.8.8.8 +PACKETMARK=1 +GATEWAY_IP=$1 +NUMPROBES=4 +THRESHOLD=2 + +RECEIVED=$(ping -m"$PACKETMARK" -I "$GATEWAY_IP" -c "$NUMPROBES" "$TARGET" | grep -oP '\d+(?= received)') + +if [ "$RECEIVED" -ge "$THRESHOLD" ]; then + echo 1 +else + echo 0 +fi diff --git a/scripts/route-table42-reachable6.sh b/scripts/route-table42-reachable6.sh new file mode 100755 index 0000000000000000000000000000000000000000..08c1232ca3b726c1d7811bc9af7bba9ee8b0dee3 --- /dev/null +++ b/scripts/route-table42-reachable6.sh @@ -0,0 +1,14 @@ +#!/bin/sh +TARGET=2001:4860:4860::8888 +PACKETMARK=1 +GATEWAY_IP6=$1 +NUMPROBES=4 +THRESHOLD=2 + +RECEIVED=$(ping6 -m"$PACKETMARK" -I "$GATEWAY_IP6" -c "$NUMPROBES" "$TARGET" | grep -oP '\d+(?= received)') + +if [ "$RECEIVED" -ge "$THRESHOLD" ]; then + echo 1 +else + echo 0 +fi diff --git a/zabbix_agentd.conf.d/userparameter.conf b/zabbix_agentd.conf.d/userparameter.conf new file mode 100644 index 0000000000000000000000000000000000000000..c2f4e0fb3e4b508337bde2b3cf0e7ff2afcb7469 --- /dev/null +++ b/zabbix_agentd.conf.d/userparameter.conf @@ -0,0 +1,16 @@ +UserParameter=route-table42-reachable,sudo /etc/zabbix/scripts/route-table42-reachable.sh +UserParameter=route-table42-reachable6,sudo /etc/zabbix/scripts/route-table42-reachable6.sh +UserParameter=dhcp.pool.all,dhcpd-pools -c /etc/dhcp/dhcpd.conf -l /var/lib/dhcp/dhcpd.leases -L22 +UserParameter=dhcp.pool.max[*],dhcpd-pools -c /etc/dhcp/dhcpd.conf -l /var/lib/dhcp/dhcpd.leases -L22|grep -i $1|sed 's/ \+/;/g'|cut -d';' -f2 +UserParameter=dhcp.pool.use[*],dhcpd-pools -c /etc/dhcp/dhcpd.conf -l /var/lib/dhcp/dhcpd.leases -L22|grep -i $1|sed 's/ \+/;/g'|cut -d';' -f3 +UserParameter=dhcp.pool.percent[*],dhcpd-pools -c /etc/dhcp/dhcpd.conf -l /var/lib/dhcp/dhcpd.leases -L22|grep -i $1|sed 's/ \+/;/g'|cut -d';' -f4 +UserParameter=dhcp.pool.touch[*],dhcpd-pools -c /etc/dhcp/dhcpd.conf -l /var/lib/dhcp/dhcpd.leases -L22|grep -i $1|sed 's/ \+/;/g'|cut -d';' -f5 +UserParameter=dhcp.pool.discovery,/etc/zabbix/scripts/dhcppools.sh +UserParameter=dhcp_server_running,/etc/zabbix/scripts/dhcp_server_running.sh +UserParameter=ip_conntrack_count,/etc/zabbix/scripts/conntrack_count.sh +UserParameter=ip_conntrack_max,/etc/zabbix/scripts/conntrack_max.sh +UserParameter=batman-gateway-mode-enabled,sudo /etc/zabbix/scripts/batman-gateway-mode-enabled.sh +UserParameter=batman-maximum-gateway-metric,sudo /etc/zabbix/scripts/batman-maximum-gateway-metric.sh +UserParameter=batman-visible-gateway-count,sudo /etc/zabbix/scripts/batman-visible-gateway-count.sh +UserParameter=batman-visible-originators,sudo /etc/zabbix/scripts/batman_originators.sh +UserParameter=radvd_adv_count,sudo /etc/zabbix/scripts/radvd_adv_count \ No newline at end of file