Skip to content

Commit

Permalink
Added MongoDB custom monitoring script
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwee-Veen authored Apr 28, 2024
1 parent 49b8296 commit c35ce31
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions mongo-mem.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`

INSTANCE_ID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)

USEDMEMORY=$(free -m | awk 'NR==2{printf "%.2f\t", $3*100/$2 }')
TCP_CONN=$(netstat -an | wc -l)
TCP_CONN_PORT_27017=$(netstat -an | grep 27017 | wc -l)
SSH_TCP_CONN_PORT_22=$(netstat -an | grep 22 | wc -l)
IO_WAIT=$(iostat | awk 'NR==4 {print $5}')

aws cloudwatch put-metric-data --metric-name memory-usage --dimensions Instance=$INSTANCE_ID --namespace "Custom" --value $USEDMEMORY
aws cloudwatch put-metric-data --metric-name Tcp_connections --dimensions Instance=$INSTANCE_ID --namespace "Custom" --value $TCP_CONN
aws cloudwatch put-metric-data --metric-name TCP_connection_on_port_27017 --dimensions Instance=$INSTANCE_ID --namespace "Custom" --value $TCP_CONN_PORT_27017
aws cloudwatch put-metric-data --metric-name IO_WAIT --dimensions Instance=$INSTANCE_ID --namespace "Custom" --value $IO_WAIT

ssh_active=0
if [[ $SSH_TCP_CONN_PORT_22 > 0 ]]
then
ssh_active=1
fi
aws cloudwatch put-metric-data --metric-name SSH_Connection_Active --dimensions Instance=$INSTANCE_ID --namespace "Custom" --value $ssh_active

high_load=0
if [[ $IO_WAIT > 70 || $USEDMEMORY > 80 ]]
then
high_load=1
fi
aws cloudwatch put-metric-data --metric-name High_Load --dimensions Instance=$INSTANCE_ID --namespace "Custom" --value $high_load

0 comments on commit c35ce31

Please sign in to comment.