Created
January 15, 2018 18:36
-
-
Save nuriel77/e8c23f363379decf105f38df0e741760 to your computer and use it in GitHub Desktop.
IOTA Full Node Check script
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
#!/bin/bash | |
function usage(){ | |
cat <<EOF | |
This script will run curl commands to the API endpoint. | |
If any of the tests fails this script will return failure. | |
-a [address] API endpoint | |
-t [seconds] Seconds until connection timeout | |
-n [integer] Minimum neighbors to expect | |
-i Ignore/skip REMOTE_LIMIT_API commands check | |
-h Print help and exit | |
example: | |
$0 -a http://host-name.example.com:14265 -t 3 | |
EOF | |
} | |
while getopts ":a:t:n:ih" opt; do | |
case "${opt}" in | |
a) | |
ADDRESS=$OPTARG | |
;; | |
h) | |
usage | |
exit 0 | |
;; | |
i) | |
IGNORE_REMOTE_LIMIT_API=1 | |
;; | |
n) | |
MINIMUM_NEIGHBORS=$OPTARG | |
;; | |
t) | |
TIMEOUT=$OPTARG | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument" >&2 | |
exit 3 | |
;; | |
*) | |
usage | |
exit 3 | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [[ -z "$ADDRESS" ]]; then | |
echo "ERROR: Missing address" | |
exit 3 | |
fi | |
TIMEOUT=${TIMEOUT:-5} | |
MINIMUM_NEIGHBORS=${MINIMUM_NEIGHBORS:-2} | |
API_VERSION=1.4.1.6 | |
PAYLOAD='{"command": "getNodeInfo"}' | |
REMOTE_LIMIT_API=(getNeighbors addNeighbors removeNeighbors attachToTangle setApiRateLimit interruptAttachingToTangle) | |
# check node info | |
DATA=$(curl --retry 2 -m $TIMEOUT -s "$ADDRESS" -H "X-IOTA-API-Version: $API_VERSION" -H 'Content-Type: application/json' -d "$PAYLOAD") | |
RC=$? | |
if [[ $RC -ne 0 ]]; then | |
if [[ $RC -eq 28 ]]; then | |
echo "Operation timed out" | |
exit 2 | |
elif [[ $RC -eq 7 ]]; then | |
echo "Connection refused" | |
exit 2 | |
else | |
echo "Error contacting host, exited $RC" | |
exit 2 | |
fi | |
fi | |
LMI=$(echo "$DATA"| jq -r .latestMilestoneIndex) | |
LSSMI=$(echo "$DATA"| jq -r .latestSolidSubtangleMilestoneIndex) | |
NEIGHBORS=$(echo "$DATA"| jq -r .neighbors) | |
APP_VERSION=$(echo "$DATA"| jq -r .appVersion) | |
# check node info | |
if [[ "$API_VERSION" != "$APP_VERSION" ]]; then | |
echo "Host app version should be '$API_VERSION' but is '$APP_VERSION'" | |
exit 2 | |
elif [[ $LMI -ne $LSSMI ]]; then | |
echo "No sync: latestMilestoneIndex: $LMI and latestSolidSubtangleMilestoneIndex: $LSSMI" | |
exit 2 | |
elif [[ $NEIGHBORS -lt $MINIMUM_NEIGHBORS ]]; then | |
echo "Too few neighbors: $NEIGHBORS" | |
exit 2 | |
fi | |
if [[ -n "$IGNORE_REMOTE_LIMIT_API" ]]; then | |
exit 0 | |
fi | |
# check limited commands | |
for cmd in "${REMOTE_LIMIT_API[@]}"; do | |
PAYLOAD="{\"command\": \"$cmd\"}" | |
CODE=$(curl --retry 2 -m $TIMEOUT -s "$ADDRESS" -w "%{http_code}" -H "X-IOTA-API-Version: $API_VERSION" -H 'Content-Type: application/json' -d "$PAYLOAD" -o /dev/null) | |
RC=$? | |
if [[ $RC -eq 28 ]]; then | |
echo "Operation timed out" | |
exit 2 | |
elif [[ $RC -eq 7 ]]; then | |
echo "Connection refused" | |
exit 2 | |
elif [[ $CODE -ne 401 ]]; then | |
echo "Error: command '$cmd' is not blocked on this node" | |
exit 2 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment