Guest User

/etc/zabbix/symbol_info.sh

a guest
Jul 14th, 2021
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. TARGETDIR=/home/symbol/target
  4. WORKDIR=/home/symbol/.zabbix
  5. MYADDRESS=NATR4OJ2ROAMZLRCPWYLOC7PDW57KK75TKDG3KQ
  6.  
  7. LOGDIR=$TARGETDIR/nodes/node/logs/
  8. HARVESTINGCONFIG=$TARGETDIR/nodes/node/server-config/resources/config-harvesting.properties
  9. PRESETFILE=$TARGETDIR/preset.yml
  10. HARVESTED=$WORKDIR/harvested
  11. ORPHANED=$WORKDIR/orphaned
  12. TOTALBALANCE=$WORKDIR/totalbalance
  13. TOTALIMPORTANCE=$WORKDIR/totalimportance
  14. MYBALANCE=$WORKDIR/mybalance
  15. BLOCKS=$WORKDIR/blocks
  16. PATH=/usr/local/bin:$PATH
  17.  
  18. update_harvesting_data() {
  19.   LOGS=`find $LOGDIR -mtime -1 -type f -name 'catapult_server*.log'`
  20.   YESTERDAY=`date -u "+%Y-%m-%d %H:%M:%S" -d yesterday`
  21.   grep -a -h "successfully harvested block" $LOGS |
  22.     awk "\$0 > \"$YESTERDAY\" {print \$10 \" \" \$13}" >$HARVESTED
  23.  
  24.   PREVIOUSLYFINALIZED=0
  25.   if [ -f $WORKDIR/finalization ]
  26.   then
  27.     PREVIOUSLYFINALIZED=`cat $WORKDIR/finalization`
  28.   fi
  29.   FINALIZATIONHEIGHT=`wget -q -O- http://127.0.0.1:3000/chain/info |
  30.     jq -r .latestFinalizedBlock.height`
  31.  
  32.   if [ "$FINALIZATIONHEIGHT" -gt "$PREVIOUSLYFINALIZED" ]
  33.   then
  34.     MYBENEFICIARYADDRESS=`grep beneficiaryAddress: $PRESETFILE |
  35.       awk '{print $2}'`
  36.  
  37.     mv -f $ORPHANED ${ORPHANED}.old 2>/dev/null
  38.     awk '{print $1}' <$HARVESTED |
  39.       while read BLOCK
  40.        do
  41.         if [ "$BLOCK" -le "$PREVIOUSLYFINALIZED" ]
  42.         then
  43.       grep "^$BLOCK\$" ${ORPHANED}.old
  44.         elif [ "$BLOCK" -le "$FINALIZATIONHEIGHT" ]
  45.         then
  46.           BENEFICIARY=`wget -q -O- http://127.0.0.1:3000/blocks/$BLOCK |
  47.             jq -r .block.beneficiaryAddress`
  48.           BENEFICIARYADDRESS=`symbol-cli converter hexToBase32Address -a "$BENEFICIARY" |
  49.         tr -d -`
  50.           if [ "$BENEFICIARYADDRESS" != "$MYBENEFICIARYADDRESS" ]
  51.           then
  52.             echo $BLOCK
  53.           fi
  54.         fi
  55.       done >$ORPHANED
  56.  
  57.     echo $FINALIZATIONHEIGHT >$WORKDIR/finalization
  58.   fi
  59. }
  60.  
  61.  
  62. exclusive_lock_harvested() {
  63.   ( flock -x 9
  64.     if [ ! -f $HARVESTED ]
  65.     then
  66.       update_harvesting_data
  67.     else
  68.       MTIME=`stat -c %y $HARVESTED`
  69.       TENMINUTESAGO=`date "+%Y-%m-%d %H:%M:%S" -d "10 minutes ago"`
  70.       if [[ "$MTIME" < "$TENMINUTESAGO" ]]
  71.       then
  72.         update_harvesting_data
  73.       fi
  74.     fi
  75.     $1
  76.   ) 9>$WORKDIR/harvested.lock
  77. }
  78.  
  79.  
  80. update_balance_data() {
  81.   BALANCE=0
  82.   IMPORTANCE=0
  83.   touch $WORKDIR/addressescache
  84.   wget -q -O- http://127.0.0.1:3000/node/unlockedaccount |
  85.     jq -r .unlockedAccount |
  86.     tr -d ' "[],' |
  87.     grep -v '^$' |
  88.     while read REMOTEPUBLICKEY
  89.     do
  90.       ADDRESS=`grep $REMOTEPUBLICKEY $WORKDIR/addressescache | awk '{print $2}'`
  91.       if [ -z "$ADDRESS" ]
  92.       then
  93.         REMOTEADDRESS=`symbol-cli converter publicKeyToAddress -n MAIN_NET -p "$REMOTEPUBLICKEY" |
  94.           tr -d -`
  95.         PUBLICKEY=`wget -q -O- http://127.0.0.1:3000/accounts/$REMOTEADDRESS |
  96.           jq -r .account.supplementalPublicKeys.linked.publicKey`
  97.     if [ -n "$PUBLICKEY" ]
  98.     then
  99.       ADDRESS=`symbol-cli converter publicKeyToAddress -n MAIN_NET -p "$PUBLICKEY" |
  100.             tr -d -`
  101.           echo $REMOTEPUBLICKEY $ADDRESS >>$WORKDIR/addressescache
  102.         fi
  103.       fi
  104.       if [ -n "$ADDRESS" ]
  105.       then
  106.         wget -q -O- http://127.0.0.1:3000/accounts/$ADDRESS >$WORKDIR/addressinfo
  107.         XYM=`jq -r '.account.mosaics[] | select(.id == "6BED913FA20223F8") | .amount' <$WORKDIR/addressinfo`
  108.         XYM=$(( $XYM / 1000000 ))
  109.         if [ "$ADDRESS" == "$MYADDRESS" ]
  110.         then
  111.           echo $XYM >$MYBALANCE
  112.         fi
  113.         BALANCE=$(( $BALANCE + $XYM ))
  114.     echo $BALANCE >$TOTALBALANCE
  115.         IMP=`jq -r .account.importance <$WORKDIR/addressinfo`
  116.         IMPORTANCE=$(( $IMPORTANCE + $IMP ))
  117.         echo $IMPORTANCE >$TOTALIMPORTANCE
  118.       fi
  119.     done
  120. }
  121.  
  122.  
  123. exclusive_lock_balance() {
  124.   ( flock -x 9
  125.     if [ ! -f $TOTALBALANCE ]
  126.     then
  127.       update_balance_data
  128.     else
  129.       MTIME=`stat -c %y $TOTALBALANCE`
  130.       HALFANHOURAGO=`date "+%Y-%m-%d %H:%M:%S" -d "30 minutes ago"`
  131.       if [[ "$MTIME" < "$HALFANHOURAGO" ]]
  132.       then
  133.         update_balance_data
  134.       fi
  135.     fi
  136.     $1
  137.   ) 9>$WORKDIR/balance.lock
  138. }
  139.  
  140.  
  141. update_blocks_data () {
  142.   HEIGHT=`wget -q -O- http://127.0.0.1:3000/chain/info | jq -r .height`
  143.   HEIGHTMINUS60=$(( $HEIGHT - 60 ))
  144.   if [ ! -f $BLOCKS ]
  145.   then
  146.     OLDHEIGHT=$HEIGHTMINUS60
  147.   else
  148.     awk "\$1 > \"$HEIGHTMINUS60\" {print \$0}" <$BLOCKS >${BLOCKS}.new
  149.     OLDHEIGHT=`tail -n 1 ${BLOCKS}.new | awk '{print $1}'`
  150.   fi
  151.   H=$(( $OLDHEIGHT + 1 ))
  152.   while [ $H -le $HEIGHT ]
  153.   do
  154.     FEEMULTIPLIER=`wget -q -O- http://127.0.0.1:3000/blocks/$H | jq -r .block.feeMultiplier`
  155.     if [ $FEEMULTIPLIER -eq 0 ]
  156.     then
  157.       FEEMULTIPLIER=100
  158.     fi
  159.     echo -e "$H\t$FEEMULTIPLIER" >>${BLOCKS}.new
  160.     H=$(( $H + 1 ))
  161.   done
  162.   mv ${BLOCKS}.new $BLOCKS
  163. }
  164.  
  165.  
  166. exclusive_lock_blocks() {
  167.   ( flock -x 9
  168.     if [ ! -f $BLOCKS ]
  169.     then
  170.       update_blocks_data
  171.     else
  172.       MTIME=`stat -c %y $BLOCKS`
  173.       TWOMINUTESAGO=`date "+%Y-%m-%d %H:%M:%S" -d "2 minutes ago"`
  174.       if [[ "$MTIME" < "$TWOMINUTESAGO" ]]
  175.       then
  176.         update_blocks_data
  177.       fi
  178.     fi
  179.     $1
  180.   ) 9>$WORKDIR/block.lock
  181. }
  182.  
  183.  
  184. if [ ! -d $WORKDIR ]
  185. then
  186.   mkdir -p $WORKDIR || exit 1
  187. fi
  188.  
  189.  
  190. if [ "$1" == "harvested" ]
  191. then
  192.   if [ "$2" == "me" ]
  193.   then
  194.     MYLINKEDPUBLICKEY=`wget -q -O- http://127.0.0.1:3000/accounts/$MYADDRESS |
  195.       jq -r .account.supplementalPublicKeys.linked.publicKey`
  196.     exclusive_lock_harvested "grep $MYLINKEDPUBLICKEY $HARVESTED" | wc -l
  197.   else
  198.     exclusive_lock_harvested "wc -l $HARVESTED" | awk '{print $1}'
  199.   fi
  200.  
  201. elif [ "$1" == "orphaned" ]
  202. then
  203.   exclusive_lock_harvested "wc -l $ORPHANED" | awk '{print $1}'
  204.  
  205. elif [ "$1" == "maxUnlockedAccounts" ]
  206. then
  207.   grep maxUnlockedAccounts $HARVESTINGCONFIG | awk '{print $3}'
  208.  
  209. elif [ "$1" == "broker_is_running" ]
  210. then
  211.   ps axu 2>/dev/null | grep /usr/catapult/bin/catapult.broker | grep -v grep | wc -l
  212.  
  213. elif [ "$1" == "catapult_is_running" ]
  214. then
  215.   ps axu 2>/dev/null | grep /usr/catapult/bin/catapult.server | grep -v grep | wc -l
  216.  
  217. elif [ "$1" == "mongodb_is_running" ]
  218. then
  219.   ps axu 2>/dev/null | grep mongodb | grep -v grep | wc -l
  220.  
  221. elif [ "$1" == "rest_is_running" ]
  222. then
  223.   ps axu 2>/dev/null | grep /symbol-workdir/rest.json | grep -v grep | wc -l
  224.  
  225. elif [ "$1" == "totalbalance" ]
  226. then
  227.   exclusive_lock_balance "cat $TOTALBALANCE"
  228.  
  229. elif [ "$1" == "totalimportance" ]
  230. then
  231.   exclusive_lock_balance "cat $TOTALIMPORTANCE"
  232.  
  233. elif [ "$1" == "mybalance" ]
  234. then
  235.   exclusive_lock_balance "cat $MYBALANCE"
  236.  
  237.  
  238. elif [ "$1" == "fee" ]
  239. then
  240.  
  241.   if [ "$2" == "min" ]
  242.   then
  243.     exclusive_lock_blocks "sort -k 2 -n $BLOCKS" | head -n 1 | awk '{print $2}'
  244.  
  245.   elif [ "$2" == "max" ]
  246.   then
  247.     exclusive_lock_blocks "sort -k 2 -n $BLOCKS" | tail -n 1 | awk '{print $2}'
  248.  
  249.   elif [ "$2" == "median" ]
  250.   then
  251.     exclusive_lock_blocks "cut -f 2 $BLOCKS" | numaverage -M
  252.  
  253.   elif [ "$2" == "average" ]
  254.   then
  255.     exclusive_lock_blocks "cut -f 2 $BLOCKS" | numaverage
  256.  
  257.   elif [ "$2" == "below100" ]
  258.   then
  259.     exclusive_lock_blocks "cat $BLOCKS" | awk "\$2 < 100 {print \$2}" | wc -l
  260.   fi
  261. fi
RAW Paste Data