Skip to content

Commit

Permalink
iprovement
Browse files Browse the repository at this point in the history
  • Loading branch information
shunanya committed Dec 12, 2013
1 parent fcc432b commit c472c1e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 118 deletions.
101 changes: 45 additions & 56 deletions MySQL monitor/monitor/monitis_api.sh
Expand Up @@ -19,7 +19,7 @@ source monitis_constant.sh || error 2 monitis_const
# http://monitis.som/api?action=authToken&apikey=[yourAPIKey]&secretkey=[yourSecretKey]
#
function get_token() {
force=${1:-$FALSE}
local force=${1:-$FALSE}
local val="."
MSG=""

Expand All @@ -28,7 +28,7 @@ function get_token() {
then
local action="api?action=$API_GET_TOKEN_ACTION&apikey=$APIKEY&secretkey=$SECRETKEY&version=$APIVERSION"
local req="$SERVER$action"
local response="$(curl -Gs $req)"
response="$(curl -Gs $req)"
if [[ (${#response} -gt 0) && (${#response} -lt 200) ]] # Normally, the response text length shouldn't exceed 200 chars
then # Likely, we received correct answer - parsing
val=`jsonval $response $API_GET_TOKEN_ACTION `
Expand Down Expand Up @@ -182,7 +182,7 @@ function add_custom_monitor {

if [[ ( -z "$data") ]]
then
MSG='add_custom_monitor - NO RESPONSE DATA??'
MSG="add_custom_monitor - NO RESPONSE DATA??"
return 3
fi

Expand Down Expand Up @@ -213,7 +213,7 @@ function get_custom_monitor_info() {
id=`jsonval "$response" "id" `
if [[ (-n $id) && ($id -eq $monitor_id) ]]
then
MSG=$response
MSG="$response"
else
MSG="Monitor with ID \"$monitor_id\" is not exist"
return 3
Expand All @@ -231,18 +231,21 @@ function get_custom_monitor_info() {
}

# Returns the specified custom monitors list
# @param $1 - tag to get monitors for
# @param $2 - type of the monitor
# @param $1 - monitor name
# @param $2 - monitor tag
# @param $3 - monitor type
#
# return result in 'response' global variables
# exit codes:
# 0 - success
# 1 - response contain more than 1000 chars
# 3 - response contains no any monitor id
function get_monitors_list() {
local monitor_tag=${1:-""}
local monitor_type=${2:-""}
MSG=""
local monitor_name=${1:-""}
local monitor_tag=${2:-""}
local monitor_type=${3:-""}

local ret=0

# GET request permanent paramenters
local permdata=`get_permanent_get_param`
Expand All @@ -255,30 +258,34 @@ function get_monitors_list() {
if [[ (-n "$monitor_type") ]] ; then
postdata=$postdata" -d type=$monitor_type "
fi
if [[ (-n "$monitor_name") ]] ; then
postdata=$postdata" -d name=$monitor_name "
fi

req="$SERVER""$API_PATH"

response="$(curl -Gs $permdata $postdata $req)"

if [[ (${#response} -gt 0) && (${#response} -lt 1000) ]] # Normally, the response text length shouldn't exceed 1000 chars
if [[ (${#response} -gt 10) && (${#response} -lt 1000) ]] # Normally, the response text length shouldn't exceed 1000 chars
then # Likely, we received correct answer
#parsing
id=`jsonval "$response" "id" `
if [[ (-z $id) ]]
then
MSG="get_monitors_list - Response contains no any ID: \"$response\""
return 3
isJSONarray "$response"
ret="$?"
if [[ ($ret -ne 0) ]] ; then # not array
MSG="get_monitors_list - response is not an array"
ret=3
fi
else
if [[ (${#response} -le 0) ]]
then
MSG="get_monitors_list - No response received..."
return 1
ret=3
else
MSG="get_monitors_list - Response is too long..."
MSG="get_monitors_list - Unclear response..."
ret=1
fi
fi
return 0
return $ret
}

# Returns the specified custom monitor ID (if exist)
Expand All @@ -290,52 +297,32 @@ function get_monitorID {
local tag=${2:-""}
local type=${3:-""}

if [[ (-n $name) && (-n $tag) && (-n $type) ]]
then
get_monitors_list "$tag" "$type"
local ret=0

if [[ (-n $name) && (-n $tag) && (-n $type) ]] ; then
get_monitors_list "$name" "$tag" "$type"
ret="$?"
if [[ ($ret -ne 0) ]]
then
return $ret
if [[ ($ret -ne 0) ]] ; then
ret=$ret
else
isJSONarray "$response"
ret="$?"
if [[ ($ret -ne 0) ]]
then # not array
MSG="get_monitorID - Not an array"
isJSON "$response"
ret="$?"
if [[ ($ret -ne 0) ]]
then
MSG="get_monitorID - Not a Json"
fi
else #array
#tmp=$(echo $response | replace "[{" "{" | replace "}]" "}" | replace "}," "} | " | replace "{" " {" | replace "})" "} )" )
tmp=`jsonArray2ss "${response}" `
tmp=`jsonArray2ss "${response}" ` #convert json array to set of json objects separated by "|"
set -- "$tmp"
OIFS=$IFS
IFS="|"
declare -a Array=($*)
IFS=$OIFS
for (( i=0 ; i< "${#Array[@]}" ; i++ ))
do
value=`jsonval "${Array[$i]}" "name" `
if [[ ("$?" -eq 0) ]]
then # Found name
if [[ (${value#"name:"} == $name) ]]
then
value=`jsonval "${Array[$i]}" "id" `
if [[ ( ${#Array[@]} -eq 1 ) ]] ; then
value=`jsonval "${Array[0]}" "id" `
MSG="OK"
ret="$?"
echo $value
return $ret
fi
fi
done
ret=$ret
else
MSG="get_monitorID - Monitor not found in response list"
fi
fi
fi
return 1
return $ret
}

# adds data for a custom monitor
Expand All @@ -346,6 +333,7 @@ function get_monitorID {
function add_custom_monitor_data() {
local results=${1:-""}
local timestamp=${2:-$(get_timestamp)}
local ret=0
MSG=""

# POST request permanent paramenters
Expand All @@ -368,19 +356,20 @@ function add_custom_monitor_data() {
then # status is ok
MSG="$TRUE"
else
MSG='add_custom_monitor_data: response - '$response
return 1
MSG="add_custom_monitor_data: response - $response"
ret=1
fi
else
if [[ (${#response} -le 0) ]]
then
MSG="add_custom_monitor_data: No response received.."
return 1
ret=3
else
MSG="add_custom_monitor_data: Response is too long..."
ret=3
fi
fi
return 0
return $ret
}

# adds additional data for a custom monitor
Expand Down Expand Up @@ -414,7 +403,7 @@ function add_custom_monitor_additional_data() {
then # status is ok
MSG="$TRUE"
else
MSG='add_custom_monitor_additional_data. Response - '$response
MSG="add_custom_monitor_additional_data. Response - $response"
return 1
fi
else
Expand Down
8 changes: 6 additions & 2 deletions MySQL monitor/monitor/monitis_util.sh
Expand Up @@ -7,6 +7,7 @@
# to their hexadecimal character representations (as defined in RFC3986)
# " " "!" "#" "$" "&" "'" "(" ")" ":" "/" "?" "[" "]" "@" "*" "+" "," ";" "="
uri_escape(){
# echo -E "$@" | sed 's/ /%20/g;s/!/%21/g;s/"/%22/g;s/#/%23/g;s/\$/%24/g;s/\&/%26/g;s/'\''/%27/g;s/(/%28/g;s/)/%29/g;s/:/%3A/g;s/\[/%5B/g;s/\]/%5D/g;s/,/%2C/g;s/;/%3B/g;s/\./%2E/g'
echo -E "$@" | sed 's/ /%20/g;s/!/%21/g;s/"/%22/g;s/#/%23/g;s/\$/%24/g;s/\&/%26/g;s/'\''/%27/g;s/(/%28/g;s/)/%29/g;s/:/%3A/g;s/\[/%5B/g;s/\]/%5D/g;s/,/%2C/g;s/;/%3B/g'
}

Expand Down Expand Up @@ -113,10 +114,13 @@ function jsonval() {
local prop=${2:-""}
if [[ (-n $json) && (-n $prop) ]]
then
temp=`echo $json | sed 's/\\\\\//\//g' | sed -e 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w -m1 $prop`
echo ${temp#*:}
local temp=`echo $json | sed 's/\\\\\//\//g' | sed -e 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w -m1 $prop`
temp="${temp#*:}"
if [[ (-n "$temp") ]] ; then
echo "$temp"
return 0
fi
fi
return 1
}

Expand Down
92 changes: 35 additions & 57 deletions MySQL monitor/monitor/mymon_start.sh
Expand Up @@ -28,8 +28,6 @@ do
shift
done

DURATION=$((60*$DURATION)) #convert to sec

echo "***$NAME - Monitor start with following parameters***"
echo "Monitor name = $MONITOR_NAME"
echo "Monitor tag = $MONITOR_TAG"
Expand All @@ -41,58 +39,43 @@ echo "Previous status file = $FILE_STATUS_PREV"
echo "Duration for sending info = $DURATION sec"
echo "Sending into $SERVER"

echo obtaining TOKEN
get_token
ret="$?"
if [[ ($ret -ne 0) ]]
then
error 3 "$MSG"
else
echo $NAME - RECEIVE TOKEN: "$TOKEN" at `date -u -d @$(( $TOKEN_OBTAIN_TIME/1000 ))` >&2
echo "All is OK for now."
fi

if [[ ($MONITOR_ID -gt 0) ]]
then
echo "$NAME - Monitor ID \"${MONITOR_ID}\" isn't ZERO - try to check correctness." >&2
get_custom_monitor_info "$MONITOR_ID"
ret="$?"
if [[ ($ret -ne 0) ]]
then # not found monitor with given ID
echo "$NAME - Monitor ID is incorrect - it cannot be used" >&2
MONITOR_ID=0
else
echo "$NAME - Monitor ID is correct - we will use it" >&2
fi
fi

if [[ ($MONITOR_ID -le 0) ]]
then
echo $NAME - Adding custom monitor with parameters name: "$MONITOR_NAME" tag: "$MONITOR_TAG" type: "$MONITOR_TYPE" params: "$RESULT_PARAMS" >&2
add_custom_monitor "$MONITOR_NAME" "$MONITOR_TAG" "$RESULT_PARAMS" "$ADDITIONAL_PARAMS" "$MONITOR_TYPE"
ret=1
while [ $ret -ne 0 ] ; do
echo obtaining TOKEN
get_token
ret="$?"
if [[ ($ret -ne 0) ]] ; then
error "$ret" "$NAME - $MSG"
else
echo $NAME - Custom monitor id = "$MONITOR_ID" >&2
replaceInFile "monitis_global.sh" "MONITOR_ID" "$MONITOR_ID"
echo "All is OK for now."
fi
fi
done
echo $NAME - RECEIVE TOKEN: "$TOKEN" at `date -u -d @$(( $TOKEN_OBTAIN_TIME/1000 ))` >&2
echo "All is OK for now."

if [[ ($MONITOR_ID -le 0) ]] ; then
echo $NAME - MonitorId is still zero - try to obtain it from Monitis >&2

MONITOR_ID=`get_monitorID "$MONITOR_NAME" "$MONITOR_TAG" "$MONITOR_TYPE" `
ret="$?"
if [[ ($ret -ne 0) ]]
then
error "$ret" "$NAME - $MSG"
else
echo $NAME - Custom monitor id = "$MONITOR_ID" >&2
replaceInFile "monitis_global.sh" "MONITOR_ID" "$MONITOR_ID"
echo "All is OK for now."
fi
DURATION=$((60*$DURATION)) #convert to sec

#trying to get monitor id
id=`get_monitorID "$MONITOR_NAME" "$MONITOR_TAG" "$MONITOR_TYPE" `
ret="$?"
if [[ ($ret -ne 0) ]] ; then
error 1 "$NAME - $MSG ( $ret )"
#try to add new monitor
echo $NAME - Adding custom monitor >&2
add_custom_monitor "$MONITOR_NAME" "$MONITOR_TAG" "$RESULT_PARAMS" "$ADDITIONAL_PARAMS" "$MONITOR_TYPE"
ret="$?"
if [[ ($ret -ne 0) ]] ; then
error "$ret" "$NAME - $MSG"
else
echo $NAME - Custom monitor id = "$MONITOR_ID" >&2
replaceInFile "monitis_global.sh" "MONITOR_ID" "$MONITOR_ID"
echo "All is OK for now."
fi
else
if [[ ($MONITOR_ID -le 0) || ($MONITOR_ID -ne $id) ]] ; then
MONITOR_ID=$id
replaceInFile "monitis_global.sh" "MONITOR_ID" "$MONITOR_ID"
fi
echo $NAME - Custom monitor id = "$MONITOR_ID" >&2
echo "All is OK for now."
fi

# Periodically adding new data
Expand Down Expand Up @@ -120,7 +103,7 @@ do
param=` trim $param `
param=` uri_escape $param `
echo
echo $NAME - DEBUG: Composed params is \"$param\" >&2
echo $NAME - DEBUG: Composed params is \"$param\"
echo
timestamp=`get_timestamp`

Expand All @@ -142,10 +125,6 @@ do
fi

param=$(echo ${result} | awk -F "|" '{print $2}' )
param=$(trim "$param")
#echo
#echo Additional param = "$param"
#echo
unset array
OIFS=$IFS
IFS='+'
Expand All @@ -154,15 +133,15 @@ do
array_length="${#array[@]}"
if [[ ($array_length -gt 0) ]] ; then
echo
echo $NAME - DEBUG: Composed additional params from \"${array[@]}\" >&2
echo $NAME - DEBUG: Composed additional params from \"${array[@]}\"
echo
param=`create_additional_param array[@] `
ret="$?"
if [[ ($ret -ne 0) ]] ; then
error "$ret" "$param"
else
echo
echo $NAME - DEBUG: Composed additional params is \"$param\" >&2
echo $NAME - DEBUG: Composed additional params is \"$param\"
echo

# Sending to Monitis
Expand All @@ -178,6 +157,5 @@ do
echo "$NAME - ****No any detailed records yet ($array_length)"
fi
fi

done

0 comments on commit c472c1e

Please sign in to comment.