_common.sh 10 KB

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