zabbix.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/sh
  2. echo -e '\033[35m
  3. _____ __ __ _
  4. /__ / ____ _/ /_ / /_ (_) __
  5. / / / __ `/ __ \/ __ \/ / |/_/
  6. / /__/ /_/ / /_/ / /_/ / /> <
  7. /____/\__,_/_.___/_.___/_/_/|_|
  8. \033[0m'
  9. if [ "$EUID" -ne 0 ]; then
  10. echo "Please run as root"
  11. exit
  12. fi
  13. # get the current position
  14. _cwd="$(pwd)"
  15. # check for assets forlder
  16. _assets="$_cwd/assets"
  17. if [ ! -d "$_assets" ]; then
  18. _assets="$_cwd/../assets"
  19. if [ ! -d "$_assets" ]; then
  20. echo "!! can't find assets directory !!"
  21. exit
  22. fi
  23. fi
  24. wget -P /tmp/ wget https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian12_all.deb
  25. dpkg -i /tmp/zabbix-release_6.4-1+debian12_all.deb
  26. apt-get update -y
  27. apt-get install zabbix-agent -y
  28. # configure
  29. echo -n "Please provide the current server's public ip : "
  30. read _cur_ip
  31. echo -n "Please provide the zabbix-server's ip : "
  32. read _ip
  33. echo -n "Please provide the hostname of this agent : "
  34. read _host_name
  35. _agent_conf_d="/etc/zabbix/zabbix_agentd.d" # for debian 8
  36. if [ ! -d "$_agent_conf_d" ]; then
  37. _agent_conf_d="/etc/zabbix/zabbix_agentd.conf.d" # for debian 9
  38. fi
  39. # configure zabbix agent
  40. sed -i "s#Server=127.0.0.1#Server=$_ip#g" /etc/zabbix/zabbix_agentd.conf
  41. sed -i "s#ServerActive=127.0.0.1#ServerActive=$_ip#g" /etc/zabbix/zabbix_agentd.conf
  42. sed -i "s#Hostname=Zabbix server#Hostname=$_host_name#g" /etc/zabbix/zabbix_agentd.conf
  43. # todo ask if LXC container, if yes install this script
  44. # https://github.com/kvaps/zabbix-linux-container-template
  45. # APT
  46. # check for debian security updates
  47. # not working : https://www.osso.nl/blog/zabbix-counting-security-updates
  48. # https://github.com/theranger/zabbix-apt
  49. # enable automatic update of apt
  50. cp "$_assets"/zabbix/misc/02periodic /etc/apt/apt.conf.d/
  51. cp "$_assets"/zabbix/apt.conf "$_agent_conf_d"/
  52. # MYSQL
  53. # https://serverfault.com/questions/737018/zabbix-user-parameter-mysql-status-setting-home
  54. # create zabbix user home
  55. echo -n "monitor mysql? [Y|n] "
  56. read yn
  57. yn=${yn:-y}
  58. if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
  59. echo -n "Please provide the mysql root password : "
  60. read _root_mysql_passwd
  61. mkdir /var/lib/zabbix
  62. # generate random password for zabbix mysql user
  63. _passwd="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c12)"
  64. # add mysql credentials to zabbix home
  65. printf "[client]\n
  66. user=zabbix\n
  67. password=$_passwd" > /var/lib/zabbix/.my.cnf
  68. # create zabbix mysql user
  69. mysql -uroot -p"$_root_mysql_passwd" -e "CREATE USER 'zabbix' IDENTIFIED BY '$_passwd';"
  70. mysql -uroot -p"$_root_mysql_passwd" -e "GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY '$_passwd';"
  71. # add zabbix-agent parameter
  72. cp "$_assets"/zabbix/userparameter_mysql.conf "$_agent_conf_d"/
  73. fi
  74. # NGINX
  75. # https://github.com/sfuerte/zbx-nginx
  76. # nginxconf already included in default.nginxconf asset
  77. echo -n "Monitor nginx? [Y|n] "
  78. read yn
  79. yn=${yn:-y}
  80. if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
  81. sed -i "s/# allow CURRENT-SERVER-IP/allow $_cur_ip/g" /etc/nginx/sites-available/default
  82. cp "$_assets"/zabbix/userparameter_nginx.conf "$_agent_conf_d"/
  83. mkdir /etc/zabbix/zabbix_agentd.scripts
  84. cp "$_assets"/zabbix/scripts/nginx-stat.py /etc/zabbix/zabbix_agentd.scripts/
  85. chmod +x /etc/zabbix/zabbix_agentd.scripts/nginx-stat.py
  86. fi
  87. echo -n "This is box is a proxmox CT? [Y|n] "
  88. read yn
  89. yn=${yn:-y}
  90. if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
  91. cp "$_assets"/zabbix/proxmox-ct.conf "$_agent_conf_d"/
  92. fi
  93. # SYSTEMD
  94. # https://github.com/MogiePete/zabbix-systemd-service-monitoring
  95. cp "$_assets"/zabbix/userparameter_systemd_services.conf "$_agent_conf_d"/
  96. # disble unused system units
  97. systemctl disable rsync
  98. systemctl disable systemd-timesyncd
  99. # TODO add modules path to agent ??
  100. # allow comm. port with zabbix-server
  101. ufw allow from "$_ip" to any port 10050
  102. ufw allow from "$_ip" to any port 22
  103. # ufw allow from "$_ip" to any port 10051
  104. # iptables -A INPUT -p tcp -m tcp --dport 10050 -j ACCEPT
  105. systemctl restart zabbix-agent
  106. systemctl enable zabbix-agent
  107. echo -e "\033[92;1mZabbix-agent installed and configured, please add the host $_host_name in your zabbix-server \033[Om"
  108. echo -e "\033[92;1mAnd import requested templates in assets/zabbix/templates/ \033[Om"
  109. echo -e "\033[92;1mzabbix user mysql password is $_passwd \033[Om"