#!/bin/bash

SERVER=127.0.0.1
PORT=80
HTTP=http
expireSeconds=5
TOKEN=

function check_url(){
    count=1
    while true
    do
        curl -k -s ${HTTP}://${SERVER}:${PORT}/api/values
        if [ $? -eq 0 ];then
             break
        else
            if [ $count -ge 3 ];then
                echo "服务异常，不能访问"
                break
            else
                let count++
                sleep 5
                continue
            fi
        fi
    done
}

function get_token(){
    TOKEN=$(curl -X POST -s -k -i -d 'account=admin&password=edoc2' ${HTTP}://${SERVER}:${PORT}/api/auth/login | grep token | head -n 1 | awk -F '[=|;]' '{print $2}')
}

function open_apm_perflog(){
   curl -X GET -s -k -i "${HTTP}://${SERVER}:${PORT}/api/services/System/SetWebSiteLogSwichConfig?IsEnable=true&token=${TOKEN}&expireSeconds=${expireSeconds}" 
   #curl -X GET -s -k -i "${HTTP}://${SERVER}:${PORT}/api/services/System/SetWebSiteLogSwichConfig?IsEnable=false&token=${TOKEN}&expireSeconds=${expireSeconds}" 
}

function main(){
    check_url
    get_token 
    open_apm_perflog 
}

main
