_common.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # ============= FUTURE YUNOHOST HELPER =============
  3. # Delete a file checksum from the app settings
  4. #
  5. # $app should be defined when calling this helper
  6. #
  7. # usage: ynh_remove_file_checksum file
  8. # | arg: file - The file for which the checksum will be deleted
  9. ynh_delete_file_checksum () {
  10. local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_'
  11. ynh_app_setting_delete $app $checksum_setting_name
  12. }
  13. #Zabbix part
  14. #===================GET GUEST DEFAULT USER STATE==============
  15. #return 0 if enable, else 1
  16. get_state_guest_user(){
  17. $mysqlconn -BN -e "SELECT count(id) from \`users_groups\` where userid=2 and usrgrpid=9"
  18. }
  19. #================ DISABLE DEFAULT ZABBIX USER GUEST ===================
  20. disable_guest_user(){
  21. if [ $(get_state_guest_user) = "0" ];then
  22. lastid=$($mysqlconn -BN -e "SELECT max(id) from \`users_groups\`")
  23. lastid=$(("$lastid" + 1 ))
  24. $mysqlconn -e "INSERT INTO \`users_groups\` (\`id\` , \`usrgrpid\`, \`userid\`) VALUES ($lastid ,9, 2);"
  25. fi
  26. }
  27. #===================GET ADMIN DEFAULT USER STATE==============
  28. #return 0 if enable, else 1
  29. get_state_admin_user(){
  30. $mysqlconn -BN -e "SELECT count(id) from \`users_groups\` where userid=1 and usrgrpid=9"
  31. }
  32. #================ DISABLE DEFAULT ADMIN USER ===================
  33. disable_admin_user(){
  34. if [ $(get_state_admin_user) = "0" ] ;then
  35. lastid=$($mysqlconn -BN -e "SELECT max(id) from \`users_groups\`")
  36. lastid=$((lastid + 1 ))
  37. $mysqlconn -e "INSERT INTO \`users_groups\` (\`id\` , \`usrgrpid\`, \`userid\`) VALUES ($lastid ,9, 1);"
  38. ynh_print_info "Default admin disabled"
  39. else
  40. ynh_print_info "Default admin already disabled"
  41. fi
  42. }
  43. enable_admin_user(){
  44. if [ $(get_state_admin_user) = "1" ] ;then
  45. ynh_print_info "Enable default admin"
  46. #enable default admin temporaly
  47. $mysqlconn -e "DELETE FROM users_groups where usrgrpid=9 and userid=1;"
  48. ynh_print_info "Default admin enabled"
  49. else
  50. ynh_print_info "Default admin already enable"
  51. fi
  52. }
  53. import_template(){
  54. ynh_print_info "Import yunohost template"
  55. zabbixFullpath=https://$domain$path_url
  56. localpath=$(find /var/cache/yunohost/ -name "Template_Yunohost.xml")
  57. sudoUserPpath=$(find /var/cache/yunohost/ -name "etc_sudoers.d_zabbix")
  58. confUserPpath=$(find /var/cache/yunohost/ -name "etc_zabbix_zabbix_agentd.d_userP_yunohost.conf")
  59. bashUserPpath=$(find /var/cache/yunohost/ -name "etc_zabbix_zabbix_agentd.d_yunohost.sh")
  60. cp "$sudoUserPpath" /etc/sudoers.d/zabbix
  61. if [ -d /etc/zabbix/zabbix_agentd.d ];then
  62. mv /etc/zabbix/zabbix_agentd.d /etc/zabbix/zabbix_agentd.conf.d
  63. fi
  64. if [ ! -L /etc/zabbix/zabbix_agentd.d ];then
  65. ln -s /etc/zabbix/zabbix_agentd.conf.d /etc/zabbix/zabbix_agentd.d
  66. fi
  67. cp "$confUserPpath" /etc/zabbix/zabbix_agentd.d/userP_yunohost.conf
  68. cp "$bashUserPpath" /etc/zabbix/zabbix_agentd.d/yunohost.sh
  69. chmod a+x /etc/zabbix/zabbix_agentd.d/yunohost.sh
  70. systemctl restart zabbix-agent
  71. curlOptions="--noproxy $domain -k -s --cookie cookiejar.txt --cookie-jar cookiejar.txt --resolve $domain:443:127.0.0.1"
  72. curl -L $curlOptions \
  73. --form "enter=Sign+in" \
  74. --form "name=Admin" \
  75. --form "password=zabbix" \
  76. "$zabbixFullpath/index.php"
  77. if [ $? -eq 0 ];then
  78. sid=$(curl $curlOptions \
  79. "$zabbixFullpath/conf.import.php?rules_preset=template" \
  80. | grep -Po 'name="sid" value="\K([a-z0-9]{16})(?=")' )
  81. importState=$(curl $curlOptions \
  82. --form "config=1" \
  83. --form "import_file=@$localpath" \
  84. --form "rules[groups][createMissing]=1" \
  85. --form "rules[templates][updateExisting]=1" \
  86. --form "rules[templates][createMissing]=1" \
  87. --form "rules[templateScreens][updateExisting]=1" \
  88. --form "rules[templateScreens][createMissing]=1" \
  89. --form "rules[templateLinkage][createMissing]=1" \
  90. --form "rules[applications][createMissing]=1" \
  91. --form "rules[items][updateExisting]=1" \
  92. --form "rules[items][createMissing]=1" \
  93. --form "rules[discoveryRules][updateExisting]=1" \
  94. --form "rules[discoveryRules][createMissing]=1" \
  95. --form "rules[triggers][updateExisting]=1" \
  96. --form "rules[triggers][createMissing]=1" \
  97. --form "rules[graphs][updateExisting]=1" \
  98. --form "rules[graphs][createMissing]=1" \
  99. --form "rules[httptests][updateExisting]=1" \
  100. --form "rules[httptests][createMissing]=1" \
  101. --form "rules[valueMaps][createMissing]=1" \
  102. --form "import=Import" \
  103. --form "backurl=templates.php" \
  104. --form "form_refresh=1" \
  105. --form "sid=${sid}" \ \
  106. "${zabbixFullpath}/conf.import.php?rules_preset=template" \
  107. | grep -c "Imported successfully")
  108. if [ "$importState" -eq "1" ];then
  109. ynh_print_info "Template Yunohost imported !"
  110. else
  111. ynh_print_warn "Template Yunohost not imported !"
  112. fi
  113. else
  114. ynh_print_warn "Admin user cannot connect interface !"
  115. fi
  116. }
  117. link_template(){
  118. #apply template to host
  119. 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')
  120. 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')
  121. 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')
  122. 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[]')
  123. if [ "$applyTemplate" -eq "$zabbixHostID" ];then
  124. ynh_print_info "Template Yunohost linked to Zabbix server !"
  125. else
  126. ynh_print_warn "Template Yunohost no linked to Zabbix server !"
  127. fi
  128. }
  129. check_proc_zabbixserver(){
  130. pgrep zabbix_server >/dev/null
  131. if [ $? -eq 0 ];then
  132. ynh_print_info "zabbix server is started !"
  133. else
  134. ynh_print_err "Zabbix Server not started, try to start it with the yunohost interface."
  135. ynh_print_err "If Zabbix Server can't start, please open a issue on https://github.com/YunoHost-Apps/zabbix_ynh/issues"
  136. fi
  137. }
  138. check_proc_zabbixagent(){
  139. pgrep zabbix_agentd >/dev/null
  140. if [ $? -eq 0 ];then
  141. ynh_print_info "zabbix agent is started"
  142. else
  143. ynh_print_err "Zabbix agent not started, try to start it with the yunohost interface."
  144. ynh_print_err "If Zabbix agent can't start, please open a issue on https://github.com/YunoHost-Apps/zabbix_ynh/issues"
  145. fi
  146. }
  147. install_zabbix_repo(){
  148. ynh_add_extra_apt_repos__3_path=$(find /var/cache/yunohost/ /etc/yunohost/apps/zabbix/ -name "ynh_add_extra_apt_repos__3" | tail -n 1)
  149. source "$ynh_add_extra_apt_repos__3_path"
  150. 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
  151. }