_common.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. cp "$confUserPpath" /etc/zabbix/zabbix_agentd.d/userP_yunohost.conf
  62. cp "$bashUserPpath" /etc/zabbix/zabbix_agentd.d/yunohost.sh
  63. chmod a+x /etc/zabbix/zabbix_agentd.d/yunohost.sh
  64. systemctl restart zabbix-agent
  65. curlOptions="-k -s --cookie cookiejar.txt --cookie-jar cookiejar.txt --resolve $domain:443:127.0.0.1"
  66. curl -L $curlOptions \
  67. --form "enter=Sign+in" \
  68. --form "name=Admin" \
  69. --form "password=zabbix" \
  70. "$zabbixFullpath/index.php"
  71. if [ $? -eq 0 ];then
  72. sid=$(curl $curlOptions \
  73. "$zabbixFullpath/conf.import.php?rules_preset=template" \
  74. | grep -Po 'name="sid" value="\K([a-z0-9]{16})(?=")' )
  75. importState=$(curl $curlOptions \
  76. --form "config=1" \
  77. --form "import_file=@$localpath" \
  78. --form "rules[groups][createMissing]=1" \
  79. --form "rules[templates][updateExisting]=1" \
  80. --form "rules[templates][createMissing]=1" \
  81. --form "rules[templateScreens][updateExisting]=1" \
  82. --form "rules[templateScreens][createMissing]=1" \
  83. --form "rules[templateLinkage][createMissing]=1" \
  84. --form "rules[applications][createMissing]=1" \
  85. --form "rules[items][updateExisting]=1" \
  86. --form "rules[items][createMissing]=1" \
  87. --form "rules[discoveryRules][updateExisting]=1" \
  88. --form "rules[discoveryRules][createMissing]=1" \
  89. --form "rules[triggers][updateExisting]=1" \
  90. --form "rules[triggers][createMissing]=1" \
  91. --form "rules[graphs][updateExisting]=1" \
  92. --form "rules[graphs][createMissing]=1" \
  93. --form "rules[httptests][updateExisting]=1" \
  94. --form "rules[httptests][createMissing]=1" \
  95. --form "rules[valueMaps][createMissing]=1" \
  96. --form "import=Import" \
  97. --form "backurl=templates.php" \
  98. --form "form_refresh=1" \
  99. --form "sid=${sid}" \ \
  100. "${zabbixFullpath}/conf.import.php?rules_preset=template" \
  101. | grep -c "Imported successfully")
  102. if [ "$importState" -eq "1" ];then
  103. ynh_print_info "Template Yunohost imported !"
  104. else
  105. ynh_print_warn "Template Yunohost not imported !"
  106. fi
  107. else
  108. ynh_print_warn "Admin user cannot connect interface !"
  109. fi
  110. }
  111. link_template(){
  112. #apply template to host
  113. tokenapi=$(curl -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')
  114. zabbixHostID=$(curl -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')
  115. zabbixTemplateID=$(curl -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')
  116. applyTemplate=$(curl -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[]')
  117. if [ "$applyTemplate" -eq "$zabbixHostID" ];then
  118. ynh_print_info "Template Yunohost linked to Zabbix server !"
  119. else
  120. ynh_print_warn "Template Yunohost no linked to Zabbix server !"
  121. fi
  122. }