_common.sh 11 KB

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