zabbix.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh
  2. echo -e '
  3. _ _ _
  4. _____ _| |__| |__(_)__
  5. |_ / _` | `_ \ `_ \ / _|
  6. /__\__,_|_.__/_.__/_\__|
  7. '
  8. . bin/checkroot.sh
  9. # get the current position
  10. _cwd="$(pwd)"
  11. # check for assets forlder
  12. _assets="$_cwd/assets"
  13. if [ ! -d "$_assets" ]; then
  14. _assets="$_cwd/../assets"
  15. if [ ! -d "$_assets" ]; then
  16. echo "!! can't find assets directory !!"
  17. exit
  18. fi
  19. fi
  20. echo -n "do you want to limit zabbix-agent to 3.4? [y|n] "
  21. read yn
  22. if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
  23. echo -e "Stick with zabbix-agent 3.4"
  24. echo "http://dl-cdn.alpinelinux.org/alpine/v3.8/main" >> /etc/apk/repositories
  25. echo "http://dl-cdn.alpinelinux.org/alpine/v3.8/community" >> /etc/apk/repositories
  26. # echo -e "zabbix-agent<3.4.99" >> /etc/apk/world
  27. apk update
  28. # apk upgrade
  29. apk add 'zabbix-agent=~3.4'
  30. else
  31. apk add zabbix-agent
  32. fi
  33. # configure
  34. # echo -n "Please provide the current server's public ip : "
  35. # read _cur_ip
  36. # https://www.cyberciti.biz/faq/how-to-find-out-the-ip-address-assigned-to-eth0-and-display-ip-only/
  37. _cur_ip=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
  38. # echo -n "Please provide the hostname of this agent : "
  39. # read _host_name
  40. _host_name=$HOSTNAME
  41. echo -n "Please provide the zabbix-server's ip : "
  42. read _ip
  43. echo -n "Please provide the mysql root password : "
  44. read _root_mysql_passwd
  45. # configure zabbix agent
  46. sed -i "s#Server=127.0.0.1#Server=$_ip#g" /etc/zabbix/zabbix_agentd.conf
  47. sed -i "s#ServerActive=127.0.0.1#ServerActive=$_ip#g" /etc/zabbix/zabbix_agentd.conf
  48. sed -i "s#Hostname=Zabbix server#Hostname=$_host_name#g" /etc/zabbix/zabbix_agentd.conf
  49. _agent_conf_d="/etc/zabbix/zabbix_agentd.d"
  50. mkdir $_agent_conf_d
  51. sed -i "s|#\ Include=$|Include= $_agent_conf_d|g" /etc/zabbix/zabbix_agentd.conf
  52. # apk
  53. # check for alpine security updates
  54. # MYSQL
  55. # https://serverfault.com/questions/737018/zabbix-user-parameter-mysql-status-setting-home
  56. # create zabbix user home
  57. mkdir /var/lib/zabbix
  58. # generate random password for zabbix mysql user
  59. _passwd="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c16)"
  60. # add mysql credentials to zabbix home
  61. printf "[client]\n
  62. user=zabbix\n
  63. password=$_passwd" > /var/lib/zabbix/.my.cnf
  64. # create zabbix mysql user
  65. mysql -uroot -p"$_root_mysql_passwd" -e "CREATE USER 'zabbix' IDENTIFIED BY '$_passwd';"
  66. mysql -uroot -p"$_root_mysql_passwd" -e "GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY '$_passwd';"
  67. # add zabbix-agent parameter
  68. cp "$_assets"/zabbix/userparameter_mysql.conf "$_agent_conf_d"/
  69. # NGINX
  70. # https://github.com/sfuerte/zbx-nginx
  71. # nginxconf already included in default.nginxconf asset
  72. sed -i "s/# allow CURRENT-SERVER-IP/allow $_cur_ip/g" /etc/nginx/conf.d/default.conf
  73. cp "$_assets"/zabbix/userparameter_nginx.conf "$_agent_conf_d"/
  74. mkdir /etc/zabbix/zabbix_agentd.scripts
  75. cp "$_assets"/zabbix/scripts/nginx-stat.py /etc/zabbix/zabbix_agentd.scripts/
  76. chmod +x /etc/zabbix/zabbix_agentd.scripts/nginx-stat.py
  77. echo -n "This is box is a proxmox CT? [Y|n] "
  78. read yn
  79. yn=${yn:-y}
  80. if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
  81. cp "$_assets"/zabbix/proxmox-ct.conf "$_agent_conf_d"/
  82. fi
  83. # allow comm. port with zabbix-server
  84. ufw allow from "$_ip" to any port 22
  85. ufw allow from "$_ip" to any port 10050
  86. # ufw allow from "$_ip" to any port 10051
  87. rc-update add zabbix-agentd
  88. service zabbix-agentd restart
  89. echo -e "Zabbix-agent installed and configured, please add the host $_host_name in your zabbix-server"
  90. echo -e "And import requested templates in assets/zabbix/templates/"
  91. # echo -e "zabbix user mysql password is $_passwd"