#! /bin/bash . /usr/src/k_framework/main.sh # ---- VARS ----- VERSION="1.0.3" SCRIPT_NAME="Zabbix Agent Check" ZABBIX_SERVER="135.181.92.61" MYSQL_ROOT_PASSWORD="" MYSQL_ROOT_PASSWORD_KNOWN=1 ZABBIX_MYSQL_SCRIPT="/usr/src/k_framework/helpers/zabbix/zabbix_mysql_setup.sh" # ---- FUNCTIONS ----- get_mysql_root_password() { local ret_value=0 if [[ $(timeout 5 /usr/bin/mysql -h "$DB_HOST" -u root -sNe "select 1 from dual" 2>/dev/null;) == 1 ]]; then MYSQL_ROOT_PASSWORD="" elif [[ $(timeout 5 /usr/bin/mysql -h "$DB_HOST" -u root --password=kolmisoft -sNe "select 1 from dual" 2>/dev/null) == 1 ]]; then MYSQL_ROOT_PASSWORD="kolmisoft" else MYSQL_ROOT_PASSWORD="" MYSQL_ROOT_PASSWORD_KNOWN=0 ret_value=1 fi if /usr/bin/timeout 5 /usr/bin/mysql -V | grep -Eq 'Distrib 5\.[15]'; then if ((MYSQL_ROOT_PASSWORD_KNOWN==1)); then if [[ $MYSQL_ROOT_PASSWORD == "" ]]; then P_R_OPT="" else P_R_OPT="-p$MYSQL_ROOT_PASSWORD" fi else P_R_OPT="-p" fi else if ((MYSQL_ROOT_PASSWORD_KNOWN==1)); then P_R_OPT="" else P_R_OPT="-p" fi fi return $ret_value } # reuturns # 0 - if old config is default # 1 - if old config is non-default (possibility that client use zabbix himself) old_config_default() { local ret_value=0 if grep -q '^Include=/usr/src/mor/' /etc/zabbix/$ZA_CONFIG; then include_file=$(grep '^Include=/usr/src/mor/' /etc/zabbix/$ZA_CONFIG | tail -n 1 | awk -F= '{print $2}') if [[ -f $include_file ]]; then # Default old Server config config if grep -q 'Server=176.31.122.225,94.23.196.71,192.168.0.12' "$include_file"|| grep -q 'Server=135.181.92.61,94.23.196.71,192.168.0.12,185.73.41.1' "$include_file"; then : else ret_value=1 fi fi fi if grep -q '^ServerActive' /etc/zabbix/$ZA_CONFIG && ! grep -q '^ServerActive=127.0.0.1' /etc/zabbix/$ZA_CONFIG; then ret_value=1 fi return $ret_value } # ---- MAIN ----- k_start k_config_details if [[ ! -d "/etc/zabbix" ]]; then report "Zabbix Agent not present" 1 report " /usr/src/k_framework/helpers/zabbix/zabbix_agent_install.sh" fix k_exit 1 fi if [ -f /usr/sbin/zabbix_agent2 ]; then # Centos7 agent2 ZA_NAME="zabbix-agent2" ZA_CONFIG="zabbix_agent2.conf" elif [ -f /etc/init.d/zabbix-agent ]; then # Centos6 agent ZA_NAME="zabbix-agent" ZA_CONFIG="zabbix_agentd.conf" elif [ -f /usr/sbin/zabbix_agentd ]; then # Centos7 agent ZA_NAME="zabbix-agent" ZA_CONFIG="zabbix_agentd.conf" else report "Can't determine Zabbix Agent name" 1 report " Contact Kolmisoft/MK for script update" fix k_exit 1 fi if [[ $ZA_NAME != "zabbix-agent2" ]]; then report "zabbix-agent is running, but we should use zabbix-agent2" 2 if ! old_config_default; then report "Old config is non default" 2 report "If zabbix is used by client too, get client permission before proceeding" 2 fi report " /usr/src/k_framework/helpers/zabbix/zabbix_agent_install.sh UPGRADE" fix k_exit 1 else report "Zabbix Agent installed" 0 fi # determine zabbix-agent process name report "Zabbix Agent name: $ZA_NAME" 3 k_service_check $ZA_NAME k_service_check_enabled $ZA_NAME # does config has our zabbix server configured? if cat /etc/zabbix/$ZA_CONFIG | grep "$ZABBIX_SERVER" | grep -q Server; then report "Server $ZABBIX_SERVER configured in $ZA_CONFIG" 0 else report "Server $ZABBIX_SERVER not configured in $ZA_CONFIG" 1 report " Add $ZABBIX_SERVER to the Server= line in $ZA_CONFIG and service $ZA_NAME restart" fix EXIT_CODE=1 fi # does config has old include? if grep -q '^Include=/usr/src/mor/' /etc/zabbix/$ZA_CONFIG; then include_file=$(grep '^Include=/usr/src/mor/' /etc/zabbix/$ZA_CONFIG | tail -n 1 | awk -F= '{print $2}') if [[ -f $include_file ]]; then report "Detected old zabbix include(s) Include=/usr/src/mor/xxxxxxx" 1 report " sed -i '/Include=\/usr\/src\/mor\/*/d' /etc/zabbix/$ZA_CONFIG" fix report " service $ZA_NAME restart" fix fi fi # get port if netstat -vatupn | grep zabbix | grep -q 10050; then report "Zabbix Agent listens on port 10050" 0 ZA_PORT="10050" else # get another port ZA_PORT=`netstat -vatupn | grep zabbix | grep ::: | awk '{print $4}' | tr -d :` if [[ $ZA_PORT != "" ]]; then report "Zabbix Agent listens on port $ZA_PORT" 0 else report "Can't get Zabbix Agent port" 1 report " Contact Kolmisoft/MK for script update" fix EXIT_CODE=1 fi fi # check if zabbix server reachable k_tcp_connection_check $ZABBIX_SERVER 5 if [[ $ZA_NAME == "zabbix-agent2" ]]; then if [[ $DB_PRESENT == 1 ]] ; then if [[ $DB_HOST != 127.0.0.1 ]] && [[ $DB_HOST != localhost ]]; then report "DB_PRESENT=1, but DB is not localhost/127.0.0.1" 3 report "Do NOT add Mysql template for this server" 3 EXIT_CODE=2 fi if ! get_mysql_root_password; then report "Neither , nor works for mysql root password" report "Enter correct root password or press Ctrl-C" fi if ! [[ $(/usr/bin/mysql -h "127.0.0.1" -u zbx_monitor -pbG6XgrYRUh7mj9hRp5JfjtgV -sNe 'select 1 from dual' 2>/dev/null) == 1 ]]; then report "User [zbx_monitor@127.0.0.1] does NOT exist" 1 report " $ZABBIX_MYSQL_SCRIPT" fix EXIT_CODE=1 else report "User [zbx_monitor@127.0.0.1] exists" 0 fi grants=$(MYSQL_PWD="$MYSQL_ROOT_PASSWORD" /usr/bin/mysql -h "$DB_HOST" -u root $P_R_OPT -sNe "SHOW GRANTS FOR zbx_monitor@127.0.0.1;" 2>/dev/null) if [[ $(echo "$grants" | tr ',' '\n' | grep -Ec 'REPLICATION CLIENT|PROCESS|SHOW DATABASES|SHOW VIEW') -lt 4 ]]; then report "User [zbx_monitor@127.0.0.1] does NOT have proper GRANTs" 1 report " $ZABBIX_MYSQL_SCRIPT" fix EXIT_CODE=1 else report "Correct grants for ['zbx_monitor'@'127.0.0.1'] exits" 0 fi fi fi if [[ $EXIT_CODE != 1 ]]; then k_default_interface_ip report "Details for Zabbix Agent configuration: $DEFAULT_IP $ZA_PORT" 3 fi k_exit $EXIT_CODE