forked from faucetsdn/udmi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automapper revamp for local mqtt and metadata extraction (faucetsdn#967)
- Loading branch information
Showing
20 changed files
with
320 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,20 @@ | ||
#!/bin/bash -e | ||
|
||
ROOT=$(realpath $(dirname $0)/..) | ||
UDMI_ROOT=$(dirname $0)/.. | ||
cd $UDMI_ROOT | ||
|
||
kubectl config current-context | tr _ ' ' > /tmp/namespace_tmp | ||
# Result is something like: gke bos-platform-dev us-central1 main grafnu | ||
read < /tmp/namespace_tmp gcp project_id region cluster namespace | ||
source $UDMI_ROOT/etc/shell_common.sh | ||
|
||
if [[ -n $1 ]]; then | ||
subscription=$1 | ||
else | ||
subscription=$namespace~debug | ||
fi | ||
echo Pulling from subscription $subscription | ||
|
||
tmp_file=/tmp/message_captured.json | ||
pull_limit=100 | ||
|
||
while true; do | ||
date | ||
gcloud --format=json --project=$project_id pubsub subscriptions pull $subscription --limit $pull_limit --auto-ack > $tmp_file || true | ||
|
||
for index in $(seq 0 $((pull_limit-1))); do | ||
msg_file=/tmp/message_$index.json | ||
raw_file=/tmp/rawdata_$index.b64 | ||
jq -r .[$index].message $tmp_file 2> /dev/null > $msg_file | ||
subType=$(jq -r .attributes.subType $msg_file 2> /dev/null) | ||
subFolder=$(jq -r .attributes.subFolder $msg_file 2> /dev/null) | ||
deviceId=$(jq -r .attributes.deviceId $msg_file 2> /dev/null) | ||
registryId=$(jq -r .attributes.deviceRegistryId $msg_file 2> /dev/null) | ||
timestamp=$(jq -r .publishTime $msg_file 2> /dev/null) | ||
raw_data=$(jq -r .data $msg_file) | ||
# There's two different base64 formats, so replace - with + to handle both. | ||
echo $raw_data > $raw_file | ||
data=$(echo $raw_data | tr - + | base64 --decode) | ||
[[ $# == 2 ]] || usage project_spec registry_id | ||
|
||
if [[ $raw_data == null || -z $raw_data ]]; then | ||
break | ||
fi | ||
project_spec=$1 | ||
registry_id=$2 | ||
shift | ||
|
||
if [[ -z $data ]]; then | ||
echo Bad/empty message data: $raw_data | ||
continue | ||
fi | ||
|
||
if [[ $subType == null ]]; then | ||
subType=event | ||
fi | ||
|
||
timepath=$(echo ${timestamp%:*} | tr T: //) # Bucket messages by minute | ||
usetime=$(echo $timestamp | tr : x) # Colon is not allowed on Windows! | ||
out_base=$ROOT/out/registries/$registryId/devices/$deviceId/${timepath}/${usetime}_${subFolder}_${subType} | ||
out_file=${out_base}.json | ||
echo $out_file | ||
mkdir -p $(dirname $out_file) | ||
echo $data | jq . > $out_file || echo $data > $out_file | ||
out_attr=${out_base}.attr | ||
jq .attributes < $msg_file > $out_attr | ||
done | ||
done | ||
if [[ $project_spec =~ //mqtt/ ]]; then | ||
bin/pull_mqtt $project_spec $registry_id | ||
elif [[ $project_spec =~ //gbos/ ]]; then | ||
bin/pull_pubsub $project_spec $registry_id | ||
else | ||
fail unknown project spec type $project_spec | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/bin/bash -e | ||
|
||
UDMI_ROOT=$(dirname $0)/.. | ||
cd $UDMI_ROOT | ||
|
||
source $UDMI_ROOT/etc/shell_common.sh | ||
|
||
[[ $# == 2 ]] || usage project_spec registry_id | ||
|
||
project_spec=$1 | ||
registry_id=$2 | ||
shift 2 | ||
|
||
msg_file=/tmp/message_captured.json | ||
|
||
source $UDMI_ROOT/etc/mosquitto_ctrl.sh | ||
|
||
topic_filter="/r/${registry_id}/d/+/#" | ||
|
||
echo Starting mqtt message capture at $(date -u -Is) on topic $topic_filter | ||
|
||
sudo mosquitto_sub $SERVER_OPTS -R -F "%j" -t $topic_filter | | ||
while read -r mqtt_message; do | ||
echo $mqtt_message > $msg_file | ||
topic=$(jq -r .topic <<< "$mqtt_message") | ||
payload=$(jq -r .payload $msg_file) | ||
timestamp=$(jq -r .tst $msg_file) | ||
json=$(jq . <<< "$payload") | ||
|
||
readarray -d '/' -t array <<< "${topic}/" | ||
registryId=${array[2]} | ||
deviceId=${array[4]} | ||
subType=${array[5]} | ||
subFolder=${array[6]} | ||
|
||
# Trim whitespace | ||
subFolder=$(echo $subFolder | xargs) | ||
|
||
[[ -n ${subFolder% } ]] || subFolder=update | ||
|
||
if [[ $subType == null ]]; then | ||
subType=events | ||
fi | ||
|
||
timepath=$(echo ${timestamp%:*} | tr T: //) # Bucket messages by minute | ||
usetime=$(echo $timestamp | tr : x) # Colon is not allowed on Windows! | ||
out_base=$UDMI_ROOT/out/registries/$registryId/devices/$deviceId/${timepath}/${usetime}_${subFolder}_${subType} | ||
out_file=${out_base}.json | ||
echo $topic $out_file | ||
mkdir -p $(dirname $out_file) | ||
echo $json > $out_file | ||
out_attr=${out_base}.attr | ||
echo {} | jq ".deviceRegistryId=\"$registryId\" | \ | ||
.subFolder=\"$subFolder\" | | ||
.subType=\"$subType\" | | ||
.deviceId=\"$deviceId\"" > $out_attr | ||
done | ||
|
||
echo Finished mqtt message capture at $(date -u -Is) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/bin/bash -e | ||
|
||
UDMI_ROOT=$(dirname $0)/.. | ||
cd $UDMI_ROOT | ||
|
||
source $UDMI_ROOT/etc/shell_common.sh | ||
|
||
[[ $# -le 2 ]] || usage project_spec suffix | ||
|
||
project_spec=${1:-} | ||
suffix=${2:-} | ||
shift 2 || true | ||
|
||
TMP_FILE=/tmp/pull_pubsub.tmp | ||
|
||
if [[ -z $project_spec ]]; then | ||
kubectl config current-context | tr _ ' ' > $TMP_FILE | ||
# Result is something like: gke bos-platform-dev us-central1 main grafnu | ||
read < $TMP_FILE scheme project_id region cluster namespace | ||
else | ||
echo $project_spec | tr / ' ' > $TMP_FILE | ||
read < $TMP_FILE schema project_id namespace | ||
fi | ||
|
||
[[ -n $namespace ]] || namespace=default | ||
|
||
echo Using project $project_id namespace $namespace | ||
|
||
[[ -n $suffix ]] || suffix=debug | ||
subscription=$namespace~${suffix} | ||
echo Pulling from subscription $subscription | ||
|
||
pull_limit=100 | ||
|
||
while true; do | ||
date | ||
gcloud --format=json --project=$project_id pubsub subscriptions pull $subscription --limit $pull_limit --auto-ack > $TMP_FILE || true | ||
|
||
for index in $(seq 0 $((pull_limit-1))); do | ||
msg_file=/tmp/message_$index.json | ||
raw_file=/tmp/rawdata_$index.b64 | ||
jq -r .[$index].message $TMP_FILE 2> /dev/null > $msg_file | ||
subType=$(jq -r .attributes.subType $msg_file 2> /dev/null) | ||
subFolder=$(jq -r .attributes.subFolder $msg_file 2> /dev/null) | ||
deviceId=$(jq -r .attributes.deviceId $msg_file 2> /dev/null) | ||
registryId=$(jq -r .attributes.deviceRegistryId $msg_file 2> /dev/null) | ||
timestamp=$(jq -r .publishTime $msg_file 2> /dev/null) | ||
raw_data=$(jq -r .data $msg_file) | ||
# There's two different base64 formats, so replace - with + to handle both. | ||
echo $raw_data > $raw_file | ||
data=$(echo $raw_data | tr - + | base64 --decode) | ||
|
||
if [[ $raw_data == null || -z $raw_data ]]; then | ||
break | ||
fi | ||
|
||
if [[ -z $data ]]; then | ||
echo Bad/empty message data: $raw_data | ||
continue | ||
fi | ||
|
||
if [[ $subType == null ]]; then | ||
subType=events | ||
fi | ||
|
||
timepath=$(echo ${timestamp%:*} | tr T: //) # Bucket messages by minute | ||
usetime=$(echo $timestamp | tr : x) # Colon is not allowed on Windows! | ||
out_base=out/registries/$registryId/devices/$deviceId/${timepath}/${usetime}_${subFolder}_${subType} | ||
out_file=${out_base}.json | ||
echo $out_file | ||
mkdir -p $(dirname $out_file) | ||
echo $data | jq . > $out_file || echo $data > $out_file | ||
out_attr=${out_base}.attr | ||
jq .attributes < $msg_file > $out_attr | ||
done | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.