_common.sh 11 KB

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