Skip to content

Commit

Permalink
net ingress, egress, cpu and memory information scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudh-devanand committed May 4, 2021
1 parent 5ef4842 commit ad03fe1
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
18 changes: 18 additions & 0 deletions scripts/memcpu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# meml=mem limit memr=mem request
# cpul=cpu limit cpur=cpu request
meml=$(cat /proc/meminfo | grep MemTotal | tr -s ' ' | cut -d ' ' -f 2 | bc)
memr=$(pmap $1 | tail -1 | tr -s ' ' | cut -d ' ' -f 2 | tr -d 'K' | bc)

#converting Kb to Mb
meml="$(( meml / 1000 )).$(( meml % 1000 )) M"
memr="$(( memr / 1000 )).$(( memr % 1000 )) M"

#converting cores to millicores
cpul=$(( $(cat /proc/cpuinfo | grep cores | cut -d ':' -f 2 | tr -d ' ' |tail -1 | bc) * 1000 ))
cpur=$(echo "$(ps -p $1 -o %cpu | tail -1 | bc )*$(( cpul / 100 ))" | bc)

#printing in .json format
echo -e "\"resources\":{\n\t\"limits\":{\n\t\t\"memory\":\"$meml\",\n\t\t\"cpu\":\"$cpul m\"\n\t},"
echo -e "\t\"requests\":{\n\t\t\"memory\":\"$memr\",\n\t\t\"cpu\":\"$cpur m\"\n\t}\n}"
109 changes: 109 additions & 0 deletions scripts/net.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/bin/bash

#setting options to filter desired protocol
opt="-unp"
prot="udp"

#while loop to get ss stdout filtered based on prot
export x=1
{
while [ $x -lt 3 ]
do
#ingrsip=ingress_ip egrsip=egress_ip
#count=counter ingrsport_ingress_port egrsport=egress_port
export ingrsip=()
export ingrsport=()
export egrsip=()
export egrsport=()
export count=0

#replacing all occurances of * with % as * is a special character
ss $opt | grep "pid=$1" | tr -s ' ' | sed 's/\*/%/g' |
{
while IFS= read -r line
do
echo $line | cut -d ' ' -f 4 > i
check=$(cat i)
#checking if ingress ip= * as format of ss stdout is different in that case
if [ "$check" = "%" ]
then
ingrsip[$count]=$(echo "*")
ingrsport[$count]=$(echo $line | cut -d ' ' -f 5)
egrsip[$count]=$(echo "*")
egrsport[$count]=$(echo $line | cut -d ' ' -f 7)

#checking if firct charecter of ip name is /
#as the ss stdout format is different then
elif [ ${check:0:1} = "/" ]
then
ingrsip[$count]=$(echo $line | cut -d ' ' -f 4)
ingrsport[$count]=$(echo $line | cut -d ' ' -f 5)
egrsip[$count]=$(echo $line | cut -d ' ' -f 6)
egrsport[$count]=$(echo $line | cut -d ' ' -f 7)
else
ingrsip[$count]=$(cut -d ':' -f 1 i)
ingrsport[$count]=$(cut -d ':' -f 2 i)
echo $line | cut -d ' ' -f 5 > o
egrsip[$count]=$(cut -d ':' -f 1 o)
egrsport[$count]=$(cut -d ':' -f 2 o)
fi
count=$((count+1))
done

ingrscount=0
egrscount=0
count=$((count-1))
if [ $count != 0 ]
then
if [ $x = 1 ]
then
echo -e "{\n ingress : [" > netingrs.json
fi
while [ $ingrscount -le $count ]
do
echo -e " {\n ip : \"${ingrsip[$ingrscount]}\" ,\n port : \"${ingrsport[$ingrscount]}\" ,\n protocol : \"$prot\"" >> netingrs.json
if [ $ingrscount = $count ]
then
echo " }" >> netingrs.json
else
echo " }," >> netingrs.json
fi
ingrscount=$((ingrscount+1))
done
if [ $x = 1 ]
then
echo -e " ],\n egress : [" > netegrs.json
fi
while [ $egrscount -le $count ]
do
echo -e " {\n ip : \"${egrsip[$egrscount]}\" ,\n port : \"${egrsport[$egrscount]}\" ,\n protocol : \"$prot\"" >> netegrs.json
if [ $egrscount = $count ]
then
echo " }" >> netegrs.json
else
echo " }," >> netegrs.json
fi

egrscount=$((egrscount+1))
done
if [ $x = 2 ]
then
echo -e " ]\n}" >> netegrs.json
fi
elif [ $x = 1 ]
then
echo -e "{\n ingress : [" > netingrs.json
echo -e " ],\n egress : [" > netegrs.json
else
echo -e "],\n}" >> netegrs.json
fi
}
prot="tcp"
opt="-ntp"
x=$((x+1))
done
}
cat netegrs.json >> netingrs.json
cat netingrs.json > netinfo.json
cat netinfo.json

0 comments on commit ad03fe1

Please sign in to comment.