Hi I need an script to read number of eth interrupts from the /proc/interrupts file with awk and find the total number of interrupts per CPU core.An then I want to use them in bash.The content of the file is;
CPU0 CPU1 CPU2 CPU3
47: 33568 45958 46028 49191 PCI-MSI-edge eth0-rx-0
48: 0 0 0 0 PCI-MSI-edge eth0-tx-0
49: 1 0 1 0 PCI-MSI-edge eth0
50: 28217 42237 65203 39086 PCI-MSI-edge eth1-rx-0
51: 0 0 0 0 PCI-MSI-edge eth1-tx-0
52: 0 1 0 1 PCI-MSI-edge eth1
59: 114991 338765 77952 134850 PCI-MSI-edge eth4-rx-0
60: 429029 315813 710091 26714 PCI-MSI-edge eth4-tx-0
61: 5 2 1 5 PCI-MSI-edge eth4
62: 1647083 208840 1164288 933967 PCI-MSI-edge eth5-rx-0
63: 673787 1542662 195326 1329903 PCI-MSI-edge eth5-tx-0
64: 5 6 7 4 PCI-MSI-edge eth5
I am reading this file with awk in this code:
#!/bin/bash
FILE="/proc/interrupts"
output=$(awk 'NR==1 {
core_count = NF
print core_count
next
}
/eth/ {
for (i = 2; i <= 2+core_count; i++)
totals[i-2] += $i
}
END {
for (i = 0; i < core_count; i++)
printf("%d\n", totals[i])
}
' $FILE)
core_count=$(echo $output | cut -d' ' -f1)
output=$(echo $output | sed 's/^[0-9]*//')
totals=(${output// / })
In this approach, I handle the total core count and then the total interrupts for per CORE in order to sort them in my script.But I can only handle the number in totals array lke this,
totals[0]=22222
totals[1]=33333
But I need to handle them as tuple with the name of the CPU cores.
totals[0]=(cPU1,2222)
totals[1]=(CPU',3333)
I think I must assign the names to an array and read themn to bash as tuples in my SED. How can I achieve this?
First of all, there’s no such thing as a ‘tuple’ in bash. And arrays are completely flat. It means that you either have a ‘scalar’ variable, or one level array of scalars.
There is a number approaches to the task you’re facing. Either:
=or:).Let’s cover approach 2 first:
Note a few things I’ve changed to make the script simpler:
totalsarray is created by flattening the output — both spaces and newlines will be treated as whitespace and use to separate the values.The resulting array looks like:
To iterate over it, you could use something like (the simple way):
Now let’s modify it for approach 1:
Note that:
totalsis declared as an associative array (declare -A),evalmust be used to let bash directly handle the output.The resulting array looks like:
And now you can use:
The third approach can be done a number of different ways. Assuming you can allow two reads of
/proc/interrupts, you could even do:So, now the awk is once again only outputting the counts, and names are obtained by bash from first line of
/proc/interruptsdirectly. Alternatively, you could create split arrays from a single array obtained in approach (2), or parsing the awk output some other way.The result would be in two arrays:
And output:
And the last approach:
Now the (regular) array looks like:
And you can parse it like:
(note now that splitting CPU name and value occurs in the loop).