Monday 30 July 2018

HTTP APIs calling and injecting Json data

#!/bin/bash
set -xvv

grouplist=$(aws autoscaling describe-auto-scaling-groups --query AutoScalingGroups[].[AutoScalingGroupName] --output text)
while read list; do
  var=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$list" --query AutoScalingGroups[].Instances[].[InstanceId] --output text)
  if [ -z "$var" ]; then continue; else
    echo $list
    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$list" --query AutoScalingGroups[].Instances[].[InstanceId] --output text
    echo ""
  fi
done <<<"$grouplist" >asop
asopa=$(perl -00 -lpe 's/\n/,/g;' asop | egrep -i 'Hotshow A')
echo $asopa | tr -s ',' '\n' >final.txt

asc=64
while read ids; do
  let asc=$asc+1
  char=$(awk 'BEGIN{printf "%c", '$asc'}')
  midtemp=$(cat jsonmid)
  count+=$(printf '%s' "$midtemp" | sed 's/i-0123456/'${ids}'/g; s/.*refId.*/"refId":"'$char'"/; /.*refId.*/ s/$/,/')
done < <(tail -n +2 final.txt)
merger=$(echo $count | sed '$ s/.$//')

value1=$(curl -H "Authorization:Bearer eyJrIjoieDZNazBVcFROZ2N1OFlJR1pvZFJiYm8wZzFqWXI3MGEiLCJuIjoiT2ZmaWNlIiwiaWQiOjF9" http://localhost/api/dashboards/id/11/versions?limit=1)
temp=$(echo $value1 | tr -s ',' '\n' | tail -4 | head -1)
jsonbot=$(sed 's/.*version.*/'$temp'/g' jsonbottom)
jsontemptop1=$(sed 's/.*version".*/'$temp'/g; /.*version".*/ s/$/,/' jsontop)
temp1=$(head -1 final.txt)
jsontemptop2=$(printf '%s' "$jsontemptop1" | sed 's/.*title.*/"title":"'"$temp1"'"/; /.*title.*/ s/$/,/')
echo $jsontemptop2$merger$jsonbot >datainject.json

curl -H "Authorization: Bearer eyJrIjoieDZNazBVcFROZ2N1OFlJR1pvZFJiYm8wZzFqWXI3MGEiLCJuIjoiT2ZmaWNlIiwiaWQiOjF9" -H "Accept: application/json" -H "Content-type: application/json" -X POST -d @datainject.json http://localhost/api/dashboards/db

rm -fr datainject* asop*

--------------------------------------------------------------------------------------------------------------------------

jsontop:

{
"meta": {
"type": "db",
"canSave": true,
"canEdit": true,
"canAdmin": true,
"canStar": true,
"slug": "apiboard",
"url": "/d/ZfrvEIKmz/Apiboard",
"expires": "0001-01-01T00:00:00Z",
"created": "2018-07-22T22:02:53Z",
"updated": "2018-07-23T12:53:11Z",
"updatedBy": "admin",
"createdBy": "admin",
"version":37,
"hasAcl": false,
"isFolder": false,
"folderId": 0,
"folderTitle": "General",
"folderUrl": "",
"provisioned": false
},
"dashboard": {
"annotations": {
"list": [{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}]
},
"description": "Data has been loaded via API Inserted",
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 11,
"links": [],
"panels": [{
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 0
},
"id": 4,
"title":"Hotshow A",
"type": "row"
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "AWS",
"fill": 1,
"gridPos": {
"h": 10,
"w": 24,
"x": 0,
"y": 1
},
"id": 2,
"legend": {
"alignAsTable": true,
"avg": true,
"current": true,
"max": true,
"min": true,
"rightSide": true,
"show": true,
"sort": "current",
"sortDesc": true,
"total": false,
"values": true
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
--------------------------------------------------------------------------------------------------------------------------

jsonmid:

{
"alias": "{{InstanceId}}",
"dimensions": {
"InstanceId": "i-0123456"
},
"highResolution": false,
"metricName": "CPUUtilization",
"namespace": "AWS/EC2",
"period": "",
"refId": "A",
"region": "ap-southeast-2",
"statistics": [
"Average"
]
},
--------------------------------------------------------------------------------------------------------------------------

jsonbottom:

],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "CPU Utilization",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [{
"format": "percent",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"refresh": "5s",
"schemaVersion": 16,
"style": "dark",
"tags": [
"final"
],
"templating": {
"list": []
},
"time": {
"from": "now-2d",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
]
},
"timezone": "browser",
"uid": "iZr08GFik",
"title": "ApiBoard",
"version":37
}
}
--------------------------------------------------------------------------------------------------------------------------