fosiul01
asked on
output of a command into an array then find the average
Hi
Theme :
(a) I am trying to take output of vmstat 2 10 to an array
(b) then find out the average for each variables
so in this question, if some one can help me to solved secion(a) , then i will create another question to solved section (b)
I have defined array VM_OUTPUT_ARRAY with 15 variables
and a funciton VMSTAT_CMD ,
so now have to run this function and have to take output into those array.
I am trying myself as well , but it would be helpful to get some hands!!!
thanks
Theme :
(a) I am trying to take output of vmstat 2 10 to an array
(b) then find out the average for each variables
so in this question, if some one can help me to solved secion(a) , then i will create another question to solved section (b)
I have defined array VM_OUTPUT_ARRAY with 15 variables
and a funciton VMSTAT_CMD ,
so now have to run this function and have to take output into those array.
I am trying myself as well , but it would be helpful to get some hands!!!
thanks
#!/bin/bash
#Version 1.0
#Status check for nagios script
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
#Define All the variables for commands
declare -rx SCRIPT=${0##*/}
declare -rx CMD_AWK="/bin/awk"
declare -rx CMD_VMSTAT="/usr/bin/vmstat"
#Define variables for holding data from vmstat output into array
declare -a VM_OUTPUT_ARRAY=($VM_r $VM_swpd $VM_free $VM_buff $VM_cache $VM_si $VM_so $VM_bi $VM_bo $VM_cs $VM_us $VM_sy $VM_id $VM_wa $VM_st)
#####################################################################
## Section 1.0 : Function-Command####################################
## This section will execute vmstat 2 5 command with defined output##
#####################################################################
function VMSTAT_CMD
{
echo $( $CMD_VMSTAT 2 5 | awk '/^.[0-9]/{print $1,$3,$4,$5,$6,$7,$8,$9,$10,$12,$13,$14,$15,$16,$17}')
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Replace your function with next and add VMSTAT_CMD and you will have
function VMSTAT_CMD
{
echo $( $CMD_VMSTAT 2 5 | grep [0-9]| awk 'BEGIN{for (i=1;i<=17;i++){s[i]=0}}{f or(j=1;j<= 17;j++){s[ j]+=$j}}EN D{for(n=1; n<17;n++){ printf("%s ", s[n]/5)}}')
}
VMSTAT_CMD
function VMSTAT_CMD
{
echo $( $CMD_VMSTAT 2 5 | grep [0-9]| awk 'BEGIN{for (i=1;i<=17;i++){s[i]=0}}{f
}
VMSTAT_CMD
ASKER
Hi
thanks thats a nice way!!!
currently the output is :
[root@web nagiosscript]# bash checkmemory.sh
0.4 0 7144 31744 75867.2 240803 0 0 2 97 818.4 157.4 0.2 1.4 98.4 0
which is fine,
but only problem is, first output which 0.4 , i know the script calculating fine. it must of got value example 2 1 0 0 0 and after it divide by 5 it got .4
but can you modify the code little bit so that it shows what out put its getting from vmstat 2 5, before calculating the average ??
does it make sense ???
again really great full
thanks thats a nice way!!!
currently the output is :
[root@web nagiosscript]# bash checkmemory.sh
0.4 0 7144 31744 75867.2 240803 0 0 2 97 818.4 157.4 0.2 1.4 98.4 0
which is fine,
but only problem is, first output which 0.4 , i know the script calculating fine. it must of got value example 2 1 0 0 0 and after it divide by 5 it got .4
but can you modify the code little bit so that it shows what out put its getting from vmstat 2 5, before calculating the average ??
does it make sense ???
again really great full
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok thank
I will close this question , and will open a new one related to this one
but with another problem
I will close this question , and will open a new one related to this one
but with another problem
ASKER
thanks
ASKER
Sorry one more thing
i am adding the modified one, small one ,
but its does not show the average of last value
the output is now :
[root@web nagiosscript]# bash checkmemory_test.sh
R-Cpu Read:2 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 Average Uses:0.4 Average Uses:0
so its showing average of R-Cpu Read
but it does not show the average of Swpd Uses
why is that ??
thanks
i am adding the modified one, small one ,
but its does not show the average of last value
the output is now :
[root@web nagiosscript]# bash checkmemory_test.sh
R-Cpu Read:2 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 R-Cpu Read:0 :Swpd Use:7144 Average Uses:0.4 Average Uses:0
so its showing average of R-Cpu Read
but it does not show the average of Swpd Uses
why is that ??
thanks
#!/bin/bash
#Version 1.0
#Status check for nagios script
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
#Define All the variables for commands
declare -rx SCRIPT=${0##*/}
declare -rx CMD_AWK="/bin/awk"
declare -rx CMD_VMSTAT="/usr/bin/vmstat"
#Define variables for holding data from vmstat output into array
declare -a VM_OUTPUT_ARRAY=($VM_r $VM_swpd)
#####################################################################
## Section 1.0 : Function-Command####################################
## This section will execute vmstat 2 5 command with defined output##
#####################################################################
function VMSTAT_CMD
{
echo $( $CMD_VMSTAT 2 5 |grep [0-9]| awk 'BEGIN{for (i=1;i<=3;i++){s[i]=0}}{print ("R-Cpu Read:"$1"\t:" "Swpd Use:"$3);for(j=1;j<=3;j++){s[j]+=$j}}END{for(n=1;n<3;n++){printf("%s ", "Average Uses:" s[n]/5)}}')
}
VMSTAT_CMD
ASKER
i need to do this on bash as need to modify/insert other options in this script
can you not help me on bash ??