send json commands with bash to boxeebox - json-rpc

I'm trying to get my callerid script to send a notification to my boxee box connected tv. I've got the script working using mgetty and notify-send on a couple of my computers.
here is my cidscript.sh which gets triggered by mgetty
#!/bin/sh
# send message to computer
ssh -o ConnectTimeout=10 mrplow#192.168.1.10 "DISPLAY=:0 notify-send 'Phone call from... $CALLER_NAME $CALLER_ID'" &
sleep 0.2
ssh -o ConnectTimeout=10 christine#192.168.1.3 "DISPLAY=:0 notify-send 'Phone call from... $CALLER_NAME $CALLER_ID'" &
sleep 0.2
ssh -o ConnectTimeout=10 mrplow#192.168.1.120 "DISPLAY=:0 notify-send 'Phone call from... $CALLER_NAME $CALLER_ID'" &
sleep 0.2
su mrplow -c "DISPLAY=:0.0 notify-send 'Phone call from... $CALLER_NAME $CALLER_ID'" &
sleep 5
# update logs
echo `date +"%F %a %r"`"|$CALLER_ID|$CALLER_NAME" >> /home/mrplow/answering_machine/logs/incoming-calls.log
scp -o ConnectTimeout=10 /home/mrplow/answering_machine/logs/incoming-calls.log christine#192.168.1.3:/home/christine/Desktop/incoming-calls.log
sleep 0.2
exit 1
I think json rpc is going to be the only way to get this to work
I've managed to telnet into the boxee box on raw port 9090 then paired my device
so the script will need to send the connect command
{"jsonrpc": "2.0", "method": "Device.Connect", "params":{"deviceid": "############"}, "id": 1}
then the actual notification
{"jsonrpc": "2.0", "method": "GUI.NotificationShow", "params":{"msg" : "Phone call from... $CALLER_NAME $CALLER_ID"}, "id": 1}
I tried this to no avail
curl -d '{"jsonrpc": "2.0", "method": "Device.Connect", "params":{"deviceid": "00112fa696c9"}, "id": 1}\
{"jsonrpc": "2.0", "method": "GUI.NotificationShow", "params":{"msg" : "test"}, "id": 1}' -i 192.168.1.6 9090

figured it out...
echo { \"jsonrpc\": \"2.0\", \"method\": \"Device.Connect\", \"params\":{\"deviceid\": \"############\"}, \"id\": 1}\
{ \"jsonrpc\": \"2.0\", \"method\": \"GUI.NotificationShow\", \"params\":{\"msg\" : \"Phone call from... $CALLER_NAME $CALLER_ID\"}, \"id\": 1 } | telnet 192.168.1.6 9090 &> /dev/null

Related

Partition JSON data using jq and then send Rest query

I have a json file like below
[
{
"field": {
"empID": "sapid",
"location": "India",
}
},
{
"field": {
"empID": "sapid",
"location": "India",
}
},
{
"field": {
"empID": "sapid",
"location": "India",
}
}
{
"field": {
"empID": "sapid",
"location": "India",
}
},
{
"field": {
"empID": "sapid",
"location": "India",
}
}
{
"field": {
"empID": "sapid",
"location": "India",
}
}
.... upto 1 million
]
I have to use this json as an input for a rest request For example
curl <REST Server URL with temp.json as input> "Content-Type: application/json" -d #temp.json
My server will not accept 1 million json object at a time.
I am looking for an approach where i have to extract the first 500 objects from the main json and send it in one rest query and then next 500 object in second rest query and so on.
Can you please suggest how can i achieve this by jq?
There's an intrinsic tradeoff here between space and time efficiency. In the following, the focus is on the latter.
Assuming that each call to curl must send a JSON array, a time-efficient solution can be constructed along the following lines:
< array.json jq -c '
def batch($n): length as $l | range(0;length;$n) as $i | .[$i: $i+n];
batch(500)
' | while read -r json
do
echo "$json" | curl -X POST -H "Content-Type: application/json" -d -# ....
done
Here .... signifies additional appropriate curl arguments.
GNU parallel
You might also want to consider using GNU parallel, e.g.:
< array.json jq -c '
def batch($n):
length as $l
| range(0;length;$n) as $i
| .[$i: $i+n];
batch(500)
' | parallel --pipe -N1 curl -X POST -H "Content-Type: application/json" -d #- ....
You have not shared any HW information of the system you are running this on. At the minimum you need to do some sort of multiprocessing to make this faster instead of running (1000000/500) curl requests altogether.
One way, would be to use GNU xargs which has a built-in to run number of parallel instances of a given process using the -P flag and number of lines of input to read from at any time with the L flag.
To start with you can do something like below to instruct curl to run on 500 lines at a time and invoke 20 such invocations in parallel. So at a given tick, approximately (500 *20) lines of input are processed. You can tune the numbers depending on your HW capability both on the host and the server side.
xargs -L 500 -P 20 curl -X POST -H "Content-Type: application/json" http://sample-url -d #- < <(jq -c 'range(0;length;500) as $i | .[$i: $i+500]' json)
Modified jq filter to pack the JSON payload as an array of objects (credit peak's answer). The earlier version jq -c '.[]' json might not work as the individual chunk of lines passed at a time doesn't represent a valid JSON.
Note: Not tested due to performance constraints.
Assuming you have this formatting, splitting can be done by unpacking the array and saving the desired number of objects to separate files, e.g.:
<input.json jq -c '.[]' | split -l500
Which creates xaa with the first 500 objects, xab with the next 500 objects, etc. If you want to repackage the objects in an array, use the -s option to jq, e.g.: jq -s . xaa.
If you want to do this from the shell, you could use jq to split your JSON and pass it to xargs to call curl for each object returned.
jq -c '.[]' temp.json | xargs -I {} curl <REST Server URL with temp.json as input> "Content-Type: application/json" -d '{}'
This will send one curl request for each object. However, if you e.g. want to send the first 500 objects in a single curl request, you can specify a subarray in the jq filter. To send all of your JSON objects you will then somehow need to repeat the command, as afaik jq has no built-in way to split the input into chunks of objects.
jq -c '.[0:500]' temp.json | xargs -I {} curl <REST Server URL with temp.json as input> "Content-Type: application/json" -d '{}'
jq -c '.[500:1000]' temp.json | xargs -I {} curl <REST Server URL with temp.json as input> "Content-Type: application/json" -d '{}'
jq -c '.[1000:1500]' temp.json | xargs -I {} curl <REST Server URL with temp.json as input> "Content-Type: application/json" -d '{}'
[...]

How to update the origination_urls when creating a new Trunk using twilio API

Thanks to this tutorial: https://www.twilio.com/docs/sip-trunking/api/trunks#action-create I am able to CRUD create, read, update and delete trunks on my Twilio account.
To create a new trunk I do it like so:
curl -XPOST https://trunking.twilio.com/v1/Trunks \
-d "FriendlyName=MyTrunk" \
-u '{twilio account sid}:{twilio auth token}'
and this is the response I get when creating a new trunk:
{
"trunks": [
{
"sid": "TKfa1e5a85f63bfc475c2c753c0f289932",
"account_sid": "ACxxx",
....
....
"date_updated": "2015-09-02T23:23:11Z",
"url": "https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932",
"links": {
"origination_urls": "https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/OriginationUrls",
"credential_lists": "https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/CredentialLists",
"ip_access_control_lists": "https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/IpAccessControlLists",
"phone_numbers": "https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/PhoneNumbers"
}
}],
"meta": {
"page": 0,
"page_size": 50,
... more
}
}
What I am interested from the response is:
"links": {
"origination_urls": "https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/OriginationUrls",
Now if I perform a get command on that link like:
curl -G "https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/OriginationUrls" -u '{twilio account sid}:{twilio auth token}'
I get back this:
{
"meta":
{
"page": 0,
"page_size": 50,
"first_page_url":
....
},
"origination_urls": []
}
Now my goal is to update the origination_urls. So using the same approach I used to update a trunk I have tried:
curl -XPOST https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/OriginationUrls \
-d "origination_urls=sip:200#somedomain.com" \
-u '{twilio account sid}:{twilio auth token}'
But that fails. I have also tried:
curl -XPOST https://trunking.twilio.com/v1/Trunks/TKfa1e5a85f63bfc475c2c753c0f289932/OriginationUrls \
-d "origination_urls=['someUrl']" \
-u '{twilio account sid}:{twilio auth token}'
and that fails too. How can I update the origination_urls?
I was missing to add Priority, FriendlyName, SipUrl, Weight and Enabled on my post request. I finally got it to work by doing:
curl -XPOST "https://trunking.twilio.com/v1/Trunks/TKfae10...../OriginationUrls" -d "Priority=10" -d "FriendlyName=Org1" -d "Enabled=true" -d "SipUrl=sip:test#domain.com" -d "Weight=10" -u '{twilio account sid}:{twilio auth token}'

Specify a httpGet and exec handler in a liveness/readiness probe

Is it possible to have multiple handlers in a container probe ?
Something like
livenessProbe: {
httpGet: {
path: "/ping",
port: 9099
},
exec: {
command: [
"verify-correctness.sh",
]
}
}
Update:
At Kube 1.6x kubectl apply for a config like this returns
spec.template.spec.containers[0].livenessProbe.httpGet: Forbidden: may not specify more than 1 handler type
So maybe not supported ?
Update 2:
After Ara Pulido's answer I combined the httpGet into the command like this:
"livenessProbe": {
"exec": {
"command": [
"sh",
"-c",
"reply=$(curl -s -o /dev/null -w %{http_code} http://127.0.0.1:9099/ping); if [ \"$reply\" -lt 200 -o \"$reply\" -ge 400 ]; then exit 1; fi; verify-correctness.sh;"
]
}
}
It is not supported.
There is an open issue about this, which contains several workarounds people use.

MInimized deployment of WSO2 APIM

We're considering to provide our own UI for WSO2, and make it work with the APIM gateway by invoking Publisher/Store REST API's.
Is there a way to strip of the UI part of WSO2 APIM and have a deployment containing only
the gateway
the key manager
the publisher --> REST API only, no UI
the store --> REST API only, no UI
Is there such bundle available out of the box?
Otherwise, will it be possible to download either the GitHub source or the deployment package and tear off any UI related plugins and their dependent libraries?
If you don't need any UI components, you may remove publisher and store jaggary web applications from repository/deployment/server/jaggaryapps location. If you checkout the source code, then you will need to checkout product repo which is in [1] and component repo which is in [2] to perform necessary changes for remove UI stuff, but it will add more complexity and take time. Without UI part, you can use REST API in 1.10.0. There is no OOTB bundle.
[1]-https://github.com/wso2/product-apim
[2]-https://github.com/wso2/carbon-apimgt
This is our script for doing this (I will thank any bug or comment, currenty we are testing it)
#!/bin/bash
# WSO2 2.1.0
# Publish an API in several gateways, using internal REST API
# Reference
# https://docs.wso2.com/display/AM210/apidocs/publisher/
# IMPORTANT: Change these values according your WSO2 APIM versionāˆ«
# Version 2.1.0
# declare APP_CLIENT_REGISTRATION="/client-registration/v0.11/register"
# declare -r URI_API_CTX="/api/am/publisher/v0.11"
# Version 2.1.0 update 14
declare -r APP_CLIENT_REGISTRATION="/client-registration/v0.12/register"
declare -r URI_API_CTX="/api/am/publisher/v0.12"
# Constants
declare -r URI_TOKEN="/token"
declare -r URI_API_APIS="${URI_API_CTX}/apis"
declare -r URI_API_ENVIRONMENTS="${URI_API_CTX}/environments"
declare -r URI_API_PUBLISH="${URI_API_CTX}/apis/change-lifecycle?action=Publish&apiId="
declare -r API_SCOPE_VIEW="apim:api_view"
declare -r API_SCOPE_PUBLISH="apim:api_publish"
declare -r API_SCOPE_CREATE="apim:api_create"
# Parameters
declare APIUSER=""
declare APIPASSWORD=""
declare APIMANAGER=""
declare APINAME=""
declare APIVERSION=""
declare -a APIGATEWAY
declare -i MANAGER_SERVICES_PORT=9443
declare -i MANAGER_NIOPT_PORT=8243
# Variables
# User login for aplication registration. User:Password in base64 (default admin:admin)
declare APIAUTH="YWRtaW46YWRtaW4="
# Client application token. ClientId:ClientSecret in base64
declare CLIENTTOKEN
# User access token (view)
declare ACCESSVIEWTOKEN
# User access token type (view)
declare ACCESSVIEWTOKENTYPE="Bearer"
# User access token (publish)
declare ACCESSPUBLISHTOKEN
# User access token type (publish)
declare ACCESSVIEWPUBLISHTYPE="Bearer"
# User access token (create)
declare ACCESSCREATETOKEN
# User access token type (create)
declare ACCESSVIEWCREATETYPE="Bearer"
# API internal ID
declare APIID
# echoErr
# Send message to error stream (/dev/stderr by default)
function echoErr() {
printf "%s\n" "$*" >&2;
}
# showHelp
# Usage info
showHelp() {
cat <<-EOF
Usage: ${0##*/} [-u USER] [-p PASSWORD] APIMANAGER [-s ServicePort] [-n NioPTPort] APINAME APIVERSION APIGATEWAY [APIGATEWAY] ...
Publish an API in the selected gateways
-u USER User name (if not defined, will ask for it)
-p PASSWORD User password (if not defined, will ask for it)
-s ServicePort Services Port in api manager host (by default 9443)
-n NioPTPort Nio/PT Port in key manager host (by default 8243)
APIMANAGER API MANAGER / KEY MANAGER host name (e.g. apimanager.example.com)
APINAME API to publish (has to be in CREATED, PROTOTYPED or PUBLISH state)
APIVERSION API Version to publish
APIGATEWAYs All of the gateway to publish the API (one or more)
EOF
}
# getPassword
# get a password type field (without echo and double input)
function getPassword()
{
local pwd=${3:-"NoSet"}
local verify="_Set_No"
local default=""
if [ -z "$1" ] || [ -z "$2" ]
then
echo 'ERROR: Use getPassword "Message" VAR_NAME [default]'
exit 1
else
if [ -n ${3} ]
then
default=$'\e[31m['${3}$']\e[0m'
fi
while true
do
read -sp "$1 $default" pwd
echo ""
# if empty (=Intro) use default if available
if [ "$pwd" == "" ] && [ -n "$3" ]
then
pwd="$3"
break
fi
# check password length
if [ ${#pwd} -lt 6 ]
then
echo "Password too short. Minimum length is 6"
continue
else
read -sp "Verify - $1 " verify
echo ""
if [ "$pwd" != "$verify" ]
then
echo "Passwords do not match. Retype."
else
break
fi
fi
done
eval $2="$pwd"
fi
}
# showGateways
# Print the list of available gateways in a friendly form
function showGateways() {
local -i count
local name
local gwtype
local endpoint
if [ -z $1 ]
then
echo "Use: showGateways \$apiEnvironments"
else
count=$(echo $1|jq -r '.count')
if [ "$count" -gt "0" ]
then
printf "%-20s %-10s %s\n" "Name" "Type" "Endpoint HTTPS" >&2
printf "%-20s %-10s %s\n" "====================" "==========" "===============================================" >&2
for i in $(seq 0 $(( $count - 1 )) )
do
name=$(echo "$1"|jq -r '.list['$i'].name')
gwtype=$(echo "$1"|jq -r '.list['$i'].type')
endpoint=$(echo "$1"|jq -r '.list['$i'].endpoints.https')
printf "%-20s %-10s %s\n" "$name" "$gwtype" "$endpoint" >&2
done
fi
fi
}
# validateGateway
# validate if all the gateways names (passed as parameter - global variable) are in environments
function validateGateways() {
if [ -z $1 ]
then
echo "Use: validateGateways \$apiEnvironments"
exit 1
else
for gateway in ${APIGATEWAY[#]}
do
jq -er \
--arg gateway_name "$gateway" '
.list[] |
select(.name == $gateway_name)
' <<<"$1" >/dev/null
if [ $? -ne 0 ]
then
echo "ERROR: Gateway '$gateway' is not found" >&2
return 1
fi
done
fi
return 0
}
# getClientToken
# Parse the answer of client registration, to get client token
# return (echo to stdout) the clientToken
function getClientToken() {
local clientId
local clientSecret
local clientToken
if [ -z $1 ]
then
echo "Use: getClientToken \$clientRegistration" >&2
exit 1
else
# Parse answer to get ClientId and ClientSecret
clientId=$(echo $clientRegistration|jq -r '.clientId')
clientSecret=$(echo $clientRegistration|jq -r '.clientSecret')
if [ "$clientId" == "" ] || [ "$clientSecret" == "" ] || [ "$clientId" == "null" ] || [ "$clientSecret" == "null" ]
then
return 1
else
echo -n "$clientId:$clientSecret"|base64
return 0
fi
fi
}
# getAccessToken
# Parse the answer of client API Login, to get client token
# return (echo to stdout) the accessToken
function getAccessToken() {
local accessToken
if [ -z $1 ]
then
echo "Use: getAccessToken \$clientAPILoginView|\$clientAPILoginPublish" >&2
exit 1
else
# Parse answer to get ClientId and ClientSecret
accessToken=$(echo $1|jq -r '.access_token')
if [ "$accessToken" == "" ] || [ "$accessToken" == "null" ]
then
return 1
else
echo -n "$accessToken"
return 0
fi
fi
}
# getAccessTokenType
# Parse the answer of client API Login, to get client token type
# return (echo to stdout) the accessTokenType
function getAccessTokenType() {
local tokenType
if [ -z $1 ]
then
echo "Use: getAccessToken \$clientAPILoginView|\$clientAPILoginPublish" >&2
exit 1
else
# Parse answer to get ClientId and ClientSecret
tokenType=$(echo $1|jq -r '.token_type')
if [ "$tokenType" == "" ] || [ "$tokenType" == "null" ]
then
return 1
else
echo -n "$tokenType"
return 0
fi
fi
}
# getAPIId
# Parse the answer of query API to get the API ID (checking version name)
# Thanks to https://stackoverflow.com/users/14122/charles-duffy
# return (echo to stdout) the APIID
function getAPIId() {
if [ -z $1 ]
then
echo "Usage: getAPIId \$apiQuery" >&2
exit 1
else
# Parse answer to get API ID
jq -er \
--arg target_name "$APINAME" \
--arg target_version "$APIVERSION" '
.list[] |
select(.name == $target_name) |
select(.version == $target_version) |
.id' <<<"$1"
fi
}
# getAPIGatewayEnvironments
# Parse the answer of detailed query API to get the API gateway environments
# return (echo to stdout) the gateway environments
function getAPIGatewayEnvironments() {
if [ -z "$1" ]
then
echo "Usage: getAPIGatewayEnvironments \$apiResource" >&2
exit 1
else
# Parse answer to get API ID
jq -er '.gatewayEnvironments' <<<"$1"
fi
}
# getAPIStatus
# Parse the answer of detailed query API to get the API status
# return (echo to stdout) the status
function getAPIStatus() {
if [ -z "$1" ]
then
echo "Usage: getAPIStatus \$apiResource" >&2
exit 1
else
# Parse answer to get API ID
jq -er '.status' <<<"$1"
fi
}
# setGateways
# Update the field gatewayEnvironments in API resource from GATEWAY parameter array
# Return the new API resource update
function setGateways() {
local gateways
local oIFS
if [ -z "$1" ]
then
echo "Use: setGateways \$apiResource" >&2
exit 1
else
oIFS="$IFS";IFS=',';gateways="${APIGATEWAY[*]}";IFS="$oIFS"
jq -e '.gatewayEnvironments="'$gateways'"' <<<$1
fi
}
# checkGateways
# check if the gateways has been updated correctly
function checkGateways() {
local gateways
local apiResourceGateways
local oIFS
if [ -z "$1" ]
then
echo "Use: checkGateways \$apiResourceUpdated" >&2
exit 1
else
oIFS="$IFS";IFS=',';gateways="${APIGATEWAY[*]}";IFS="$oIFS"
apiResourceGateways=$(echo $1|jq -r '.gatewayEnvironments')
# Return value
if [ -z "$apiResourceGateways" ] || [ "$apiResrouceGateways" == "null" ]
then
return 1
fi
# TODO: The gateways are sorted in different manner (reverse as API Manager??)
#if [ "$gateways" != "$apiResourceGateways" ]
#then
# return 1
#fi
fi
return 0
}
# getParms
# Parse the parms and assign to variables
function getParms() {
local OPTIND=1
while getopts hu:p: opt $#
do
case $opt in
h)
showHelp
exit 0
;;
u)
APIUSER=$OPTARG
;;
p)
APIPASSWORD=$OPTARG
;;
s)
MANAGER_SERVICES_PORT=$OPTARG
;;
n)
MANAGER_NIOPT_PORT=$OPTARG
;;
*)
showHelp >&2
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Discard the options and get parameter
APIMANAGER=$1
if [ "$APIMANAGER" == "" ]
then
echo "APIMANAGER host name is required"
showHelp >&2
exit 1
fi
shift 1
APINAME=$1
if [ "$APINAME" == "" ]
then
echo "API name to publish is required"
showHelp >&2
exit 1
fi
shift 1
APIVERSION=$1
if [ "$APIVERSION" == "" ]
then
echo "API version to publish is required"
showHelp >&2
exit 1
fi
shift 1
if [ "$1" == "" ]
then
echo "You must indicate 1 or more gateway to publish is required"
showHelp >&2
exit 1
else
local i=1
for arg in $#
do
APIGATEWAY[$i]="$1"
let i=(i+1)
shift 1
done
fi
}
###############################################################################
# Check required internal tools
if ! type -t jq >/dev/null
then
echo "jq not found. Install it, e.g. 'apt-get install jq'"
exit 2
fi
# Read and parse Parms. Request required values missing
getParms $#
if [ "$APIUSER" == "" ]
then
APIUSER=admin
read -p $'Publisher user: \e[31m['${APIUSER}$']\e[0m ' parm
APIUSER=${parm:-$APIUSER}
fi
if [ "$APIPASSWORD" == "" ]
then
APIPASSWORD=admin
read -sp $'Publisher password: \e[31m['${APIPASSWORD}$']\e[0m ' parm
APIPASSWORD=${parm:-$APIPASSWORD}
echo ""
fi
# TEST ONLY: Delete (show parameter values)
# echo "USER=$APIUSER"
# echo "PASSWORD=$APIPASSWORD"
# echo "APIMANAGER=$APIMANAGER"
# echo "APINAME=$APINAME"
# for GWY in ${!APIGATEWAY[#]}
# do
# echo "APIGATEWAY[$GWY]=${APIGATEWAY[$GWY]}"
# done
# Client registration
echo "Registering this script as a client application (rest_api_publisher)"
APIAUTH=$(echo -n $APIUSER:$APIPASSWORD|base64)
clientRegistration=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${APP_CLIENT_REGISTRATION}" \
-H "Authorization: Basic ${APIAUTH}" \
-H "Content-Type: application/json" \
-d #- <<-EOF
{
"callbackUrl": "www.google.lk",
"clientName": "rest_api_publisher",
"owner": "$APIUSER",
"grantType": "password refresh_token",
"saasApp": true
}
EOF
)
if [ "$clientRegistration" == "" ]
then
echo "ERROR: Empty answer from https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${APP_CLIENT_REGISTRATION}. Is APIMANAGER correct?" >&2
exit 3
fi
# Get Application Client Token
CLIENTTOKEN=$(getClientToken $clientRegistration)
if [ $? -ne 0 ]
then
echo $clientRegistration >&2
echo "ERROR: Cannot get ClientId/ClientSecret: Is user/password correct?" >&2
exit 4
fi
# TEST ONLY: Delete
# echo "CLIENTTOKEN=$CLIENTTOKEN"
echo "Aplication rest_api_publisher registered"
# Client Login for get Access Token (and Token Type) - View Scope
echo "Obtaining access token for API query (scope api_view)"
clientAPILoginView=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_NIOPT_PORT}${URI_TOKEN}" \
-H "Authorization: Basic ${CLIENTTOKEN}" \
-d "grant_type=password&username=${APIUSER}&password=${APIPASSWORD}&scope=${API_SCOPE_VIEW}"
)
ACCESSVIEWTOKEN=$(getAccessToken $clientAPILoginView) && ACCESSVIEWTOKENTYPE=$(getAccessTokenType $clientAPILoginView)
if [ $? -ne 0 ]
then
echo $clientAPILoginView >&2
echo "ERROR: Cannot get Access Token: Has the user '$APIUSER' in necesary role for scope ${API_SCOPE_VIEW}" >&2
exit 5
fi
# TEST ONLY: Delete
# echo "Access View Token=$ACCESSVIEWTOKEN"
# echo "Token View Type=$ACCESSVIEWTOKENTYPE"
# Client Login for get Access Token (and Token Type) - Publish Scope
echo "Obtaining access token for API publish (scope api_publish)"
clientAPILoginPublish=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_NIOPT_PORT}${URI_TOKEN}" \
-H "Authorization: Basic ${CLIENTTOKEN}" \
-d "grant_type=password&username=${APIUSER}&password=${APIPASSWORD}&scope=${API_SCOPE_PUBLISH}"
)
ACCESSPUBLISHTOKEN=$(getAccessToken $clientAPILoginPublish) && ACCESSPUBLISHTOKENTYPE=$(getAccessTokenType $clientAPILoginPublish)
if [ $? -ne 0 ]
then
echo $clientAPILoginPublish >&2
echo "ERROR: Cannot get Access Token: Has the user $APIUSER in necesary role for scope ${API_SCOPE_PUBLISH}" >&2
exit 5
fi
# TEST ONLY: Delete
# echo "Access Publish Token=$ACCESSPUBLISHTOKEN"
# echo "Token Publish Type=$ACCESSPUBLISHTOKENTYPE"
# Client Login for get Access Token (and Token Type) - Publish Scope
echo "Obtaining access token for API create (scope api_create)"
clientAPILoginCreate=$(
curl -s -X POST "https://${APIMANAGER}:${MANAGER_NIOPT_PORT}${URI_TOKEN}" \
-H "Authorization: Basic ${CLIENTTOKEN}" \
-d "grant_type=password&username=${APIUSER}&password=${APIPASSWORD}&scope=${API_SCOPE_CREATE}"
)
ACCESSCREATETOKEN=$(getAccessToken $clientAPILoginCreate) && ACCESSCREATETOKENTYPE=$(getAccessTokenType $clientAPILoginCreate)
if [ $? -ne 0 ]
then
echo $clientAPILoginCreate|jq . >&2
echo "ERROR: Cannot get Access Token: Has the user $APIUSER in necesary role for scope ${API_SCOPE_CREATE}" >&2
exit 5
fi
# TEST ONLY: Delete
# echo "Access Create Token=$ACCESSCREATETOKEN"
# echo "Token Create Type=$ACCESSCREATETOKENTYPE"
echo "All tokens obtained"
# Get API info (exists?)
echo "Checking API with name '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'"
apiQuery=$(
curl -s "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}?query=name:$APINAME" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
# TEST ONLY: Delete
# echo "apiQuery=${apiQuery}"
APIID=$(getAPIId $apiQuery)
if [ $? -ne 0 ]
then
echo $apiQuery >&2
echo "ERROR: Cannot find an API ${APINAME} with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 6
fi
echo "API Found. APIID='$APIID'"
# Get availables gateways and validate gateways names
echo "Checking if requested gateways '${APIGATEWAY[#]}' are available in '${APIMANAGER}'"
apiEnvironments=$(
curl -s "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_ENVIRONMENTS}" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
# TEST ONLY: Delete
# echo "apiEnvironments=$apiEnvironments"
if ! validateGateways $apiEnvironments
then
echo "Valid gateways are:"
showGateways $apiEnvironments
exit 7
fi
echo "API required gateways checked"
# Get API detailed info
echo "Getting API detailed info of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'"
apiResource=$(
curl -s -S -f -X GET "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
if [ $? -ne 0 ]
then
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 8
fi
# TEST ONLY: Delete
# jq . <<<$apiResource
currentGatewayEnvironments=$(getAPIGatewayEnvironments "$apiResource") && currentStatus=$(getAPIStatus "$apiResource")
if [ $? -ne 0 ]
then
jq . <<<$apiResource >&2
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 8
fi
echo "API is currently configured for gateways: '${currentGatewayEnvironments}'"
echo "API is currently in status: '${currentStatus}'"
# Update API gateways info
apiResourceUpdated=$(setGateways "$apiResource")
if [ $? -ne 0 ]
then
echo $apiResourceUpdated | jq . >&2
echo "ERROR: Cannot update gateways in API resource" >&2
exit 9
fi
# TEST ONLY: Delete
jq . <<<$apiResouceUpdated >&2
# PENDING: Update also required information (e.g., Endpoints)
# Update gateways
echo "Updating API gateways of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}' to '${APIGATEWAY[#]}'"
apiResourceUpdatedResponse=$(
curl -s -S -f -X PUT "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Content-Type: application/json" \
-H "Authorization: ${ACCESSCREATETOKENTYPE} ${ACCESSCREATETOKEN}" \
-d "$apiResourceUpdated"
)
if [ $? -ne 0 ]
then
# Retry request to show error in console
curl -s -X PUT "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Content-Type: application/json" \
-H "Authorization: ${ACCESSCREATETOKENTYPE} ${ACCESSCREATETOKEN}" \
-d "$apiResourceUpdated"|jq .
echo "ERROR: Cannot update gateways in API resource. Check API for missing information (HTTP Endpoints, ...)" >&2
exit 10
fi
# TEST ONLY: Delete
# jq . <<<$apiResourceUpdatedResponse
if ! checkGateways "$apiResourceUpdatedResponse"
then
echo $apiResourceUpdated| jq . >&2
echo "ERROR: Error updating gateways in API resource" >&2
exit 9
fi
echo "API Updated"
# Publish
echo "Publishing '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}' "
apiResource=$(
curl -s -S -f -X POST "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_PUBLISH}${APIID}" \
-H "Authorization: ${ACCESSPUBLISHTOKENTYPE} ${ACCESSPUBLISHTOKEN}"
)
if [ $? -ne 0 ]
then
echo "ERROR: Publishing '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 10
fi
echo "API Published"
# Verify status and gateways
echo "Verify API detailed info of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'"
apiResource=$(
curl -s -S -f -X GET "https://${APIMANAGER}:${MANAGER_SERVICES_PORT}${URI_API_APIS}/${APIID}" \
-H "Authorization: ${ACCESSVIEWTOKENTYPE} ${ACCESSVIEWTOKEN}"
)
if [ $? -ne 0 ]
then
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 11
fi
currentGatewayEnvironments=$(getAPIGatewayEnvironments "$apiResource") && currentStatus=$(getAPIStatus "$apiResource")
if [ $? -ne 0 ]
then
jq . <<<$apiResource >&2
echo "ERROR: Cannot get API detailed information of '${APINAME}' with version '${APIVERSION}' in '${APIMANAGER}'" >&2
exit 12
fi
echo "API is now configured for gateways: '${currentGatewayEnvironments}'"
echo "API is now in status: '${currentStatus}'"

Unable to run sensu check in a docker-compose context

I am dockerizing sensu infrastructure. Everything goes fine except the execution of checks.
I am using docker-compose according to this structure (docker-compose.yml):
sensu-core:
build: sensu-core/
links:
- redis
- rabbitmq
sensors-production:
build: sensors-production/
links:
- rabbitmq
uchiwa:
build: sensu-uchiwa
links:
- sensu-core
ports:
- "3000:3000"
rabbitmq:
build: rabbitmq/
redis:
image: redis
command: redis-server
My rabbitmq Dockerfile is pretty straightforward:
FROM ubuntu:latest
RUN apt-get -y install wget
RUN wget http://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
RUN dpkg -i erlang-solutions_1.0_all.deb
RUN wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
RUN apt-key add rabbitmq-signing-key-public.asc
RUN echo "deb http://www.rabbitmq.com/debian/ testing main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list
RUN apt-get update
RUN apt-get -y install erlang rabbitmq-server
CMD /etc/init.d/rabbitmq-server start && \
rabbitmqctl add_vhost /sensu && \
rabbitmqctl add_user sensu secret && \
rabbitmqctl set_permissions -p /sensu sensu ".*" ".*" ".*" && \
cd /var/log/rabbitmq/ && \
ls -1 * | xargs tail -f
So do the uchiwa Dockerfile:
FROM podbox/sensu
RUN apt-get -y install uchiwa
RUN echo ' \
{ \
"sensu": [ \
{ \
"name": "Sensu", \
"host": "sensu-core", \
"port": 4567, \
"timeout": 5 \
} \
], \
"uchiwa": { \
"host": "0.0.0.0", \
"port": 3000, \
"interval": 5 \
} \
}' > /etc/sensu/uchiwa.json
EXPOSE 3000
CMD /etc/init.d/uchiwa start && \
tail -f /var/log/uchiwa.log
Sensu core runs sensu-server & sensu-api. Here is his dockerfile:
FROM podbox/sensu
RUN apt-get -y install sensu
RUN echo '{ \
"rabbitmq": { \
"host": "rabbitmq", \
"vhost": "/sensu", \
"user": "sensu", \
"password": "secret" \
}, \
"redis": { \
"host": "redis", \
"port": 6379 \
}, \
"api": { \
"host": "localhost", \
"port": 4567 \
} \
}' >> /etc/sensu/config.json
CMD /etc/init.d/sensu-server start && \
/etc/init.d/sensu-api start && \
tail -f /var/log/sensu/sensu-server.log -f /var/log/sensu-api.log
sensors-production runs sensu-client along with a dumb metric, here is his Dockerfile:
FROM podbox/sensu
RUN apt-get -y install sensu
RUN echo '{ \
"rabbitmq": { \
"host": "rabbitmq", \
"vhost": "/sensu", \
"user": "sensu", \
"password": "secret" \
} \
}' >> /etc/sensu/config.json
RUN mkdir -p /etc/sensu/conf.d
RUN echo '{ \
"client": { \
"name": "wise_oracle", \
"address": "prod_sensors", \
"subscriptions": [ \
"web", "aws" \
] \
} \
' >> /etc/sensu/conf.d/client.json
RUN echo '{ \
"checks": { \
"dumb": { \
"command": "ls", \
"subscribers": [ \
"web" \
], \
"interval": 10 \
} \
} \
}' >> /etc/sensu/conf.d/dumb.json
CMD /etc/init.d/sensu-client start && \
tail -f /var/log/sensu/sensu-client.log
Running
docker-compose up -d
Everything goes OK. No errors in the logs, I can access the uchiwa dashboard, which shows me the defined client alright (keepalive requests seems to be OK). However, no check is available.
I noticed that no check request / check result is present in the log, as if the sensu server consider there is no check to run. Although, I have no idea why is that.
Could someone tell me what's going on more precisely? Thank you.
Check request/result will delivered via RabbitMQ, you can access to http://yourrabbitmqserver:15672 to see the queue and subscribed consumers.
Also make sure your server have some check.json files placed in /sensu/conf.d to schedule checks base on their interval