_common.sh 11 KB

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