Hello Monks
Looking to format the text output of db2pd command to convert memory values from KB to MB.
input file is as below:
Database Member 0 -- Active -- Up 26 days 13:26:26 -- Date 2020-07-10-
+10.11.32.332869
Database Member Memory Controller Statistics
Controller Automatic: N
Controller License Limit: N
Controller Limit Enforced: Y
Memory Limit: 26597404 KB
Current usage: 25768448 KB
HWM usage: 26003008 KB
Cached memory: 3352576 KB
Individual Memory Consumers:
Name Mem Used (KB) HWM Used (KB) Cached (KB)
========================================================
APPL-PB1 196352 601792 38272
DBMS-db2pb1 210752 210752 4736
FMP_RESOURCES 22528 22528 20736
PRIVATE 1377280 1424640 843968
DB-PB1 23961536 24333568 2444864
Desired output
>perl -nl db2mem.pl db2mem.PB1
Memory Limit : 25,974 MB
Current usage : 25,164 MB
HWM usage : 25,393 MB
Name Mem Used (MB) HWM Used (MB) Cached (MB)
========================================================
APPL-PB1 191 587 37
FMP_RESOURCES 22 22 20
PRIVATE 1,345 1,391 824
DB-PB1 23,399 23,763 2,387
Here is my ugly code.
wondering if there is better way for it and having parameter that can be given as MB or GB since our servers have huge memory all in GBs
#use strict;
#use warnings;
#-- Format memory in MB from db2pd -dbptnmem
#-- Run as db2pd -dbptnmem | perl -nl db2mem.pl
#-- or perl -nl db2mem.pl db2mem.PB1
sub commify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
print if ($. == 1);
if (/Name|===/) {
s/KB/MB/g;
print;
}
if (/Memory Limit|Current usage:|HWM usage:| Cached memory:/ && length
+($_) > 0 ){
($name, $value) =split /:/;
$value =~ s/KB//g;
printf "%-14s: %10s MB\n", $name, commify(int $value/1024);
if ($name eq "HWM usage") {
printf "\n";
}
}
if (/APPL-|DB-|FMP|PRI/) {
($name, $used, $hwm, $free) =split /\s+/;
printf "%-15s %10s %10s %10s\n", $name, commify(int $used/1024), c
+ommify( int $hwm/1024), commify( int $free/1024);
}