_common.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/bin/bash
  2. # Get guest user state
  3. #
  4. # return 0 if enable, else 1
  5. #
  6. get_state_guest_user () {
  7. $mysqlconn -BN -e "SELECT count(id) from \`users_groups\` where userid=2 and usrgrpid=9"
  8. }
  9. # Disable guest user
  10. #
  11. disable_guest_user () {
  12. if [ $(get_state_guest_user) = "0" ]
  13. then
  14. ynh_print_info --message="Disable guest user"
  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. ynh_print_info --message="Guest user disabled"
  19. else
  20. ynh_print_info --message="Guest user already disabled"
  21. fi
  22. }
  23. # Get admin user state
  24. #
  25. # return 0 if enable, else 1
  26. #
  27. get_state_admin_user () {
  28. $mysqlconn -BN -e "SELECT count(id) from \`users_groups\` where userid=1 and usrgrpid=9"
  29. }
  30. # Disable admin user
  31. #
  32. disable_admin_user () {
  33. if [ $(get_state_admin_user) = "0" ]
  34. then
  35. ynh_print_info --message="Disable admin user"
  36. lastid=$($mysqlconn -BN -e "SELECT max(id) from \`users_groups\`")
  37. lastid=$((lastid + 1 ))
  38. $mysqlconn -e "INSERT INTO \`users_groups\` (\`id\` , \`usrgrpid\`, \`userid\`) VALUES ($lastid ,9, 1);"
  39. ynh_print_info --message="Admin user disabled"
  40. else
  41. ynh_print_info --message="Admin user already disabled"
  42. fi
  43. }
  44. # Enable admin user
  45. #
  46. enable_admin_user () {
  47. if [ $(get_state_admin_user) = "1" ]
  48. then
  49. ynh_print_info --message="Enable admin user"
  50. #enable default admin temporaly
  51. $mysqlconn -e "DELETE FROM users_groups where usrgrpid=9 and userid=1;"
  52. ynh_print_info --message="Admin user enabled"
  53. else
  54. ynh_print_info --message="Admin user already enable"
  55. fi
  56. }
  57. # Import YunoHost template in the agent
  58. #
  59. import_template () {
  60. ynh_print_info --message="Import YunoHost template in the agent"
  61. zabbixFullpath=https://$domain$path
  62. localpath="../conf/Template_Yunohost.xml"
  63. sudoUserPpath="../conf/etc_sudoers.d_zabbix"
  64. confUserPpath="../conf/etc_zabbix_zabbix_agentd.d_userP_yunohost.conf"
  65. bashUserPpath="../conf/etc_zabbix_zabbix_agentd.d_yunohost.sh"
  66. cp "$sudoUserPpath" /etc/sudoers.d/zabbix
  67. chmod 400 /etc/sudoers.d/zabbix
  68. if [ -d /etc/zabbix/zabbix_agentd.d ]
  69. then
  70. mv /etc/zabbix/zabbix_agentd.d /etc/zabbix/zabbix_agentd.conf.d
  71. fi
  72. if [ ! -L /etc/zabbix/zabbix_agentd.d ]
  73. then
  74. ln -s /etc/zabbix/zabbix_agentd.conf.d /etc/zabbix/zabbix_agentd.d
  75. fi
  76. cp "$confUserPpath" /etc/zabbix/zabbix_agentd.d/userP_yunohost.conf
  77. cp "$bashUserPpath" /etc/zabbix/zabbix_agentd.d/yunohost.sh
  78. chown -R $app:$app "/etc/zabbix/zabbix_agentd.d/"
  79. chmod a+x /etc/zabbix/zabbix_agentd.d/yunohost.sh
  80. systemctl restart zabbix-agent
  81. # Temporarily enable visitors if needed...
  82. local visitors_enabled=$(ynh_permission_has_user "main" "visitors" && echo yes || echo no)
  83. if [[ $visitors_enabled == "no" ]]; then
  84. ynh_permission_update --permission "main" --add "visitors"
  85. fi
  86. curlOptions="--noproxy $domain -k -s --cookie cookiejar.txt --cookie-jar cookiejar.txt --resolve $domain:443:127.0.0.1"
  87. curl -L $curlOptions \
  88. --form "enter=Sign+in" \
  89. --form "name=Admin" \
  90. --form "password=zabbix" \
  91. "$zabbixFullpath/index.php"
  92. if [ $? -eq 0 ]
  93. then
  94. sid=$(curl $curlOptions \
  95. "$zabbixFullpath/conf.import.php?rules_preset=template" \
  96. | grep -Po 'name="sid" value="\K([a-z0-9]{16})(?=")' )
  97. importState=$(curl $curlOptions \
  98. --form "config=1" \
  99. --form "import_file=@$localpath" \
  100. --form "rules[groups][createMissing]=1" \
  101. --form "rules[templates][updateExisting]=1" \
  102. --form "rules[templates][createMissing]=1" \
  103. --form "rules[templateScreens][updateExisting]=1" \
  104. --form "rules[templateScreens][createMissing]=1" \
  105. --form "rules[templateLinkage][createMissing]=1" \
  106. --form "rules[applications][createMissing]=1" \
  107. --form "rules[items][updateExisting]=1" \
  108. --form "rules[items][createMissing]=1" \
  109. --form "rules[discoveryRules][updateExisting]=1" \
  110. --form "rules[discoveryRules][createMissing]=1" \
  111. --form "rules[triggers][updateExisting]=1" \
  112. --form "rules[triggers][createMissing]=1" \
  113. --form "rules[graphs][updateExisting]=1" \
  114. --form "rules[graphs][createMissing]=1" \
  115. --form "rules[httptests][updateExisting]=1" \
  116. --form "rules[httptests][createMissing]=1" \
  117. --form "rules[valueMaps][createMissing]=1" \
  118. --form "import=Import" \
  119. --form "backurl=templates.php" \
  120. --form "form_refresh=1" \
  121. --form "sid=${sid}" \ \
  122. "${zabbixFullpath}/conf.import.php?rules_preset=template" \
  123. | grep -c "Imported successfully")
  124. if [ "$importState" -eq "1" ]
  125. then
  126. ynh_print_info --message="YunoHost template imported !"
  127. else
  128. ynh_print_warn --message="YunoHost template not imported !"
  129. fi
  130. else
  131. ynh_print_warn --message="Admin user cannot connect to the interface !"
  132. fi
  133. if [[ $visitors_enabled == "no" ]]; then
  134. ynh_permission_update --permission "main" --remove "visitors"
  135. fi
  136. }
  137. # Link YunoHost template to Zabbix server
  138. #
  139. link_template () {
  140. ynh_print_info --message="Link YunoHost template to Zabbix server"
  141. # Temporarily enable visitors if needed...
  142. local visitors_enabled=$(ynh_permission_has_user "main" "visitors" && echo yes || echo no)
  143. if [[ $visitors_enabled == "no" ]]; then
  144. ynh_permission_update --permission "main" --add "visitors"
  145. fi
  146. #apply template to host
  147. 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')
  148. 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')
  149. 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')
  150. 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[]')
  151. if [ "$applyTemplate" -eq "$zabbixHostID" ]
  152. then
  153. ynh_print_info --message="YunoHost template linked to Zabbix server !"
  154. else
  155. ynh_print_warn --message="YunoHost template not linked to Zabbix server !"
  156. fi
  157. if [[ $visitors_enabled == "no" ]]; then
  158. ynh_permission_update --permission "main" --remove "visitors"
  159. fi
  160. }
  161. # Check if Zabbix server is started
  162. #
  163. check_proc_zabbixserver () {
  164. pgrep zabbix_server >/dev/null
  165. if [ $? -eq 0 ]
  166. then
  167. ynh_print_info --message="Zabbix server is started !"
  168. else
  169. ynh_print_err --message="Zabbix server not started, try to start it with the YunoHost interface."
  170. ynh_print_err --message="If Zabbix server can't start, please open a issue on https://github.com/YunoHost-Apps/zabbix_ynh/issues"
  171. fi
  172. }
  173. # Check if Zabbix agent is started
  174. #
  175. check_proc_zabbixagent () {
  176. pgrep zabbix_agentd >/dev/null
  177. if [ $? -eq 0 ]
  178. then
  179. ynh_print_info --message="Zabbix agent is started"
  180. else
  181. ynh_print_err --message="Zabbix agent not started, try to start it with the YunoHost interface."
  182. ynh_print_err --message="If Zabbix agent can't start, please open a issue on https://github.com/YunoHost-Apps/zabbix_ynh/issues"
  183. fi
  184. }
  185. # Update Zabbix configuration initialisation
  186. #
  187. update_initZabbixConf () {
  188. ynh_print_info --message="Update Zabbix configuration initialisation !"
  189. if [ ! -d /etc/zabbix/web ]
  190. then
  191. mkdir -p /etc/zabbix/web
  192. fi
  193. cp "../conf/etc_zabbix_web_init.zabbix.conf.php.sh" /etc/zabbix/web/init.zabbix.conf.php.sh
  194. chmod 700 /etc/zabbix/web/init.zabbix.conf.php.sh
  195. cp "../conf/etc_apt_apt.conf.d_100update_force_init_zabbix_frontend_config" /etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config
  196. ynh_print_info --message="Zabbix configuration initialisation updated !"
  197. }
  198. # Delete Zabbix configuration initialisation
  199. #
  200. delete_initZabbixConf () {
  201. ynh_print_info --message="Delete Zabbix configuration initialisation !"
  202. if [ -f /etc/zabbix/web/init.zabbix.conf.php.sh ]
  203. then
  204. ynh_secure_remove --file="/etc/zabbix/web/init.zabbix.conf.php.sh"
  205. fi
  206. if [ -f /etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config ]
  207. then
  208. ynh_secure_remove --file="/etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config"
  209. fi
  210. ynh_print_info --message="Zabbix configuration initialisation deleted !"
  211. }
  212. # Patch timeout too short for Zabbix agent if needed
  213. #
  214. change_timeoutAgent () {
  215. timeout_ok=$(grep "^Timeout" /etc/zabbix/zabbix_agentd.conf 2>/dev/null || true;)
  216. if [ -z "$timeout_ok" ]
  217. then
  218. ynh_replace_string --match_string="# Timeout=3" --replace_string="Timeout=10" --target_file=/etc/zabbix/zabbix_agentd.conf
  219. grep -C 2 "Timeout" /etc/zabbix/zabbix_agentd.conf
  220. systemctl restart zabbix-agent
  221. ynh_print_info --message="Zabbix agent timeout updated !"
  222. fi
  223. }
  224. # Update Zabbix database character set
  225. #
  226. convert_ZabbixDB () {
  227. ynh_print_info --message="Zabbix database character set will be updated !"
  228. $mysqlconn -e "ALTER DATABASE $db_name CHARACTER SET utf8 COLLATE utf8_general_ci;"
  229. for t in $($mysqlconn -BN -e "show tables";)
  230. do
  231. $mysqlconn -e "ALTER TABLE $t CONVERT TO character set utf8 collate utf8_bin;"
  232. done
  233. ynh_print_info --message="Zabbix database character set has been updated !"
  234. }
  235. # Add email media type with the YunoHost server mail.
  236. #
  237. set_mediatype_default_yunohost () {
  238. set -x
  239. if [ $($mysqlconn -BN -e "SELECT count(*) FROM media_type WHERE smtp_server LIKE 'mail.example.com' AND status=1;") -eq 1 ]
  240. then
  241. $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;"
  242. ynh_print_info --message="Default Media type added !"
  243. fi
  244. set +x
  245. }