Website/install_f2b_metrics.sh
2024-03-20 17:41:57 -05:00

92 lines
3.3 KiB
Bash

#!/bin/bash
#############################################################
#### Fail2Ban Metrics install Script for Prometheus ####
#### Centos/Redhat and Debian/Ubuntu Servers. ####
#### ####
#### Author: Phil Connor 08/27/2023 ####
#### Contact: contact@mylinux.work ####
#### Version 1.92.011924 ####
#### ####
#### To use this script chmod it to 755 ####
#### or simply type bash <filename.sh> ####
#############################################################
if ! crontab -l | grep -q "fail2ban_metrics.sh"; then
echo -e "$(crontab -u root -l)\n*/5 * * * * /usr/local/bin/fail2ban_metrics.sh > /var/lib/node_exporter/fail2ban_metrics.prom 2>&1" | crontab -u root -
fi
install_fail2_metrics() {
{
{
echo 'jails=0'
echo ''
# shellcheck disable=SC2016,SC2028
echo 'for jail in $(fail2ban-client status | grep '\''Jail list:'\'' | sed '\''s/.*Jail list:[\t ]*//;s/,//g'\'')'
echo ' do'
# shellcheck disable=SC2004,SC2016,SC2030
echo ' jails=$(($jails + 1))'
# shellcheck disable=SC2016
echo ' fail2ban-client status "$jail" | awk -F: '\'' '
# shellcheck disable=SC2016
echo ' $1 ~ /Currently failed/ {'
# shellcheck disable=SC2016,SC2154
echo ' print "fail2ban_failed_current{jail=\"'\''"$jail"'\''\"} " $2;'
echo ' }'
# shellcheck disable=SC2016
echo ' $1 ~ /Total failed/ {'
# shellcheck disable=SC2016
echo ' print "fail2ban_failed_total{jail=\"'\''"$jail"'\''\"} " $2;'
echo ' }'
# shellcheck disable=SC2016
echo ' $1 ~ /Currently banned/ {'
# shellcheck disable=SC2016
echo ' print "fail2ban_banned_current{jail=\"'\''"$jail"'\''\"} " $2;'
echo ' }'
# shellcheck disable=SC2016
echo ' $1 ~ /Total banned/ {'
# shellcheck disable=SC2016
echo ' print "fail2ban_banned_total{jail=\"'\''"$jail"'\''\"} " $2;'
echo ' }'\'' '
echo ' done | sort | awk '\'' '
echo ''
echo 'BEGIN {failc=1; failt=1; banc=1; bant=1}'
echo '/^fail2ban_failed_current/ { if (failc) {'
echo ' print "# HELP fail2ban_failed_current Current number of failures.";'
echo ' print "# TYPE fail2ban_failed_current gauge";'
echo ' failc=0;'
echo ' } }'
echo '/^fail2ban_failed_total/ { if (failt) {'
echo ' print "# HELP fail2ban_failed_total Total number of failures.";'
echo ' print "# TYPE fail2ban_failed_total counter";'
echo ' failt=0;'
echo ' } }'
echo '/^fail2ban_banned_current/ { if (banc) {'
echo ' print "# HELP fail2ban_banned_current Current number banned.";'
echo ' print "# TYPE fail2ban_banned_current gauge";'
echo ' banc=0;'
echo ' } }'
echo '/^fail2ban_banned_total/ { if (bant) {'
echo ' print "# HELP fail2ban_banned_total Total number banned.";'
echo ' print "# TYPE fail2ban_banned_total counter";'
echo ' bant=0;'
echo ' } }'
# shellcheck disable=SC2016
echo '{ print $0 }'\'' '
echo ''
echo 'cat <<EOF'
echo '# HELP fail2ban_jails Total number of jails.'
echo '# TYPE fail2ban_jails gauge'
# shellcheck disable=SC2016
echo 'fail2ban_jails $jails'
echo 'EOF'
} > /usr/local/bin/fail2ban_metrics.sh
chmod 755 /usr/local/bin/fail2ban_metrics.sh
}
}
install_fail2_metrics