_common.sh 10 KB

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