_common.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/bin/bash
  2. #=================================================
  3. # COMMON VARIABLES
  4. #=================================================
  5. pkg_dependencies="libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 php7.3 php7.3-bcmath ttf-dejavu-core php7.3-bcmath patch smistrip unzip wget fping libcap2-bin libiksemel3 libopenipmi0 libpam-cap libsnmp-base libsnmp30 snmptrapd snmpd libjs-prototype jq zabbix-server-mysql zabbix-agent zabbix-frontend-php"
  6. #Zabbix part
  7. #===================GET GUEST DEFAULT USER STATE==============
  8. #return 0 if enable, else 1
  9. get_state_guest_user(){
  10. $mysqlconn -BN -e "SELECT count(id) from \`users_groups\` where userid=2 and usrgrpid=9"
  11. }
  12. #================ DISABLE DEFAULT ZABBIX USER GUEST ===================
  13. disable_guest_user(){
  14. if [ $(get_state_guest_user) = "0" ];then
  15. lastid=$($mysqlconn -BN -e "SELECT max(id) from \`users_groups\`")
  16. lastid=$(("$lastid" + 1 ))
  17. $mysqlconn -e "INSERT INTO \`users_groups\` (\`id\` , \`usrgrpid\`, \`userid\`) VALUES ($lastid ,9, 2);"
  18. fi
  19. }
  20. #===================GET ADMIN DEFAULT USER STATE==============
  21. #return 0 if enable, else 1
  22. get_state_admin_user(){
  23. $mysqlconn -BN -e "SELECT count(id) from \`users_groups\` where userid=1 and usrgrpid=9"
  24. }
  25. #================ DISABLE DEFAULT ADMIN USER ===================
  26. disable_admin_user(){
  27. if [ $(get_state_admin_user) = "0" ] ;then
  28. lastid=$($mysqlconn -BN -e "SELECT max(id) from \`users_groups\`")
  29. lastid=$((lastid + 1 ))
  30. $mysqlconn -e "INSERT INTO \`users_groups\` (\`id\` , \`usrgrpid\`, \`userid\`) VALUES ($lastid ,9, 1);"
  31. ynh_print_info "Default admin disabled"
  32. else
  33. ynh_print_info "Default admin already disabled"
  34. fi
  35. }
  36. enable_admin_user(){
  37. if [ $(get_state_admin_user) = "1" ] ;then
  38. ynh_print_info "Enable default admin"
  39. #enable default admin temporaly
  40. $mysqlconn -e "DELETE FROM users_groups where usrgrpid=9 and userid=1;"
  41. ynh_print_info "Default admin enabled"
  42. else
  43. ynh_print_info "Default admin already enable"
  44. fi
  45. }
  46. import_template(){
  47. ynh_print_info "Import yunohost template"
  48. zabbixFullpath=https://$domain$path_url
  49. localpath=$(find /var/cache/yunohost/ -name "Template_Yunohost.xml")
  50. sudoUserPpath=$(find /var/cache/yunohost/ -name "etc_sudoers.d_zabbix")
  51. confUserPpath=$(find /var/cache/yunohost/ -name "etc_zabbix_zabbix_agentd.d_userP_yunohost.conf")
  52. bashUserPpath=$(find /var/cache/yunohost/ -name "etc_zabbix_zabbix_agentd.d_yunohost.sh")
  53. cp "$sudoUserPpath" /etc/sudoers.d/zabbix
  54. if [ -d /etc/zabbix/zabbix_agentd.d ];then
  55. mv /etc/zabbix/zabbix_agentd.d /etc/zabbix/zabbix_agentd.conf.d
  56. fi
  57. if [ ! -L /etc/zabbix/zabbix_agentd.d ];then
  58. ln -s /etc/zabbix/zabbix_agentd.conf.d /etc/zabbix/zabbix_agentd.d
  59. fi
  60. cp "$confUserPpath" /etc/zabbix/zabbix_agentd.d/userP_yunohost.conf
  61. cp "$bashUserPpath" /etc/zabbix/zabbix_agentd.d/yunohost.sh
  62. chmod a+x /etc/zabbix/zabbix_agentd.d/yunohost.sh
  63. systemctl restart zabbix-agent
  64. curlOptions="--noproxy $domain -k -s --cookie cookiejar.txt --cookie-jar cookiejar.txt --resolve $domain:443:127.0.0.1"
  65. curl -L $curlOptions \
  66. --form "enter=Sign+in" \
  67. --form "name=Admin" \
  68. --form "password=zabbix" \
  69. "$zabbixFullpath/index.php"
  70. if [ $? -eq 0 ];then
  71. sid=$(curl $curlOptions \
  72. "$zabbixFullpath/conf.import.php?rules_preset=template" \
  73. | grep -Po 'name="sid" value="\K([a-z0-9]{16})(?=")' )
  74. importState=$(curl $curlOptions \
  75. --form "config=1" \
  76. --form "import_file=@$localpath" \
  77. --form "rules[groups][createMissing]=1" \
  78. --form "rules[templates][updateExisting]=1" \
  79. --form "rules[templates][createMissing]=1" \
  80. --form "rules[templateScreens][updateExisting]=1" \
  81. --form "rules[templateScreens][createMissing]=1" \
  82. --form "rules[templateLinkage][createMissing]=1" \
  83. --form "rules[applications][createMissing]=1" \
  84. --form "rules[items][updateExisting]=1" \
  85. --form "rules[items][createMissing]=1" \
  86. --form "rules[discoveryRules][updateExisting]=1" \
  87. --form "rules[discoveryRules][createMissing]=1" \
  88. --form "rules[triggers][updateExisting]=1" \
  89. --form "rules[triggers][createMissing]=1" \
  90. --form "rules[graphs][updateExisting]=1" \
  91. --form "rules[graphs][createMissing]=1" \
  92. --form "rules[httptests][updateExisting]=1" \
  93. --form "rules[httptests][createMissing]=1" \
  94. --form "rules[valueMaps][createMissing]=1" \
  95. --form "import=Import" \
  96. --form "backurl=templates.php" \
  97. --form "form_refresh=1" \
  98. --form "sid=${sid}" \ \
  99. "${zabbixFullpath}/conf.import.php?rules_preset=template" \
  100. | grep -c "Imported successfully")
  101. if [ "$importState" -eq "1" ];then
  102. ynh_print_info "Template Yunohost imported !"
  103. else
  104. ynh_print_warn "Template Yunohost not imported !"
  105. fi
  106. else
  107. ynh_print_warn "Admin user cannot connect interface !"
  108. fi
  109. }
  110. link_template(){
  111. #apply template to host
  112. tokenapi=$(curl --noproxy $domain -k -s --resolve $domain:443:127.0.0.1 --header "Content-Type: application/json" --request POST --data '{ "jsonrpc": "2.0","method": "user.login","params": {"user": "Admin","password": "zabbix"},"id": 1,"auth": null}' "${zabbixFullpath}/api_jsonrpc.php" | jq -r '.result')
  113. zabbixHostID=$(curl --noproxy $domain -k -s --resolve $domain:443:127.0.0.1 --header "Content-Type: application/json" --request POST --data '{"jsonrpc":"2.0","method":"host.get","params":{"filter":{"host":["Zabbix server"]}},"auth":"'"$tokenapi"'","id":1}' "${zabbixFullpath}/api_jsonrpc.php" | jq -r '.result[0].hostid')
  114. zabbixTemplateID=$(curl --noproxy $domain -k -s --resolve $domain:443:127.0.0.1 --header "Content-Type: application/json" --request POST --data '{"jsonrpc":"2.0","method":"template.get","params":{"filter":{"host":["Template Yunohost"]}},"auth":"'"$tokenapi"'","id":1}' "${zabbixFullpath}/api_jsonrpc.php" | jq -r '.result[0].templateid')
  115. applyTemplate=$(curl --noproxy $domain -k -s --resolve $domain:443:127.0.0.1 --header "Content-Type: application/json" --request POST --data '{"jsonrpc":"2.0","method":"host.massadd","params":{"hosts":[{"hostid":"'"$zabbixHostID"'"}],"templates":[{"templateid":"'"$zabbixTemplateID"'"}]},"auth":"'"$tokenapi"'","id":1}' "${zabbixFullpath}/api_jsonrpc.php" | jq -r '.result.hostids[]')
  116. if [ "$applyTemplate" -eq "$zabbixHostID" ];then
  117. ynh_print_info "Template Yunohost linked to Zabbix server !"
  118. else
  119. ynh_print_warn "Template Yunohost no linked to Zabbix server !"
  120. fi
  121. }
  122. check_proc_zabbixserver(){
  123. pgrep zabbix_server >/dev/null
  124. if [ $? -eq 0 ];then
  125. ynh_print_info "zabbix server is started !"
  126. else
  127. ynh_print_err "Zabbix Server not started, try to start it with the yunohost interface."
  128. ynh_print_err "If Zabbix Server can't start, please open a issue on https://github.com/YunoHost-Apps/zabbix_ynh/issues"
  129. fi
  130. }
  131. check_proc_zabbixagent(){
  132. pgrep zabbix_agentd >/dev/null
  133. if [ $? -eq 0 ];then
  134. ynh_print_info "zabbix agent is started"
  135. else
  136. ynh_print_err "Zabbix agent not started, try to start it with the yunohost interface."
  137. ynh_print_err "If Zabbix agent can't start, please open a issue on https://github.com/YunoHost-Apps/zabbix_ynh/issues"
  138. fi
  139. }
  140. install_zabbix_repo(){
  141. ynh_install_extra_repo --repo="http://repo.zabbix.com/zabbix/4.4/debian $(lsb_release -sc) main" --key=https://repo.zabbix.com/zabbix-official-repo.key --priority=999 --name=zabbix
  142. }
  143. remove_zabbix_repo(){
  144. ynh_remove_extra_repo --name=zabbix
  145. }
  146. update_initZabbixConf(){
  147. if [ ! -d /etc/zabbix/web ] ;then mkdir -p /etc/zabbix/web ;fi
  148. cp $(find /var/cache/yunohost/ -name "etc_zabbix_web_init.zabbix.conf.php.sh") /etc/zabbix/web/init.zabbix.conf.php.sh
  149. chmod 700 /etc/zabbix/web/init.zabbix.conf.php.sh
  150. cp $(find /var/cache/yunohost/ -name "etc_apt_apt.conf.d_100update_force_init_zabbix_frontend_config") /etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config
  151. }
  152. delete_initZabbixConf(){
  153. if [ -f /etc/zabbix/web/init.zabbix.conf.php.sh ] ; then ynh_secure_remove /etc/zabbix/web/init.zabbix.conf.php.sh;fi
  154. if [ -f /etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config ] ;then ynh_secure_remove /etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config ;fi
  155. }
  156. #Patch timeout too short for zabbix agent if needed
  157. change_timeoutAgent(){
  158. timeout_ok=$(grep "^Timeout" /etc/zabbix/zabbix_agentd.conf 2>/dev/null || true;)
  159. if [ -z "$timeout_ok" ] ;then
  160. ynh_replace_string --match_string="# Timeout=3" --replace_string="Timeout=10" --target_file=/etc/zabbix/zabbix_agentd.conf
  161. grep -C 2 "Timeout" /etc/zabbix/zabbix_agentd.conf
  162. systemctl restart zabbix-agent
  163. fi
  164. }
  165. convert_ZabbixDB(){
  166. mysql --user=$db_user --password=$db_pwd --database=zabbix -e "ALTER DATABASE $db_name CHARACTER SET utf8 COLLATE utf8_general_ci;"
  167. for t in $(mysql -B -N --user=$db_user --password=$db_pwd --database=$db_name -e "show tables";)
  168. do
  169. mysql --user=$db_user --password=$db_pwd --database=$db_name -e "ALTER TABLE $t CONVERT TO character set utf8 collate utf8_bin;"
  170. done
  171. }
  172. #if not already modified, add email media type with the yunohost server mail.
  173. set_mediatype_default_yunohost(){
  174. set -x
  175. if [ $($mysqlconn -BN -e "SELECT count(*) FROM media_type WHERE smtp_server LIKE 'mail.example.com' AND status=1;") -eq 1 ] ; then
  176. $mysqlconn -BN -e "UPDATE media_type SET smtp_server = 'localhost', smtp_helo = '"$domain"', smtp_email = 'zabbix@"$domain"', smtp_port = '587', status=0 , smtp_security=1 WHERE smtp_server LIKE 'mail.example.com' AND status=1;"
  177. ynh_print_info "Default Media type added !"
  178. fi
  179. set +x
  180. }