_common.sh 9.9 KB

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