_common.sh 11 KB

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