I'm relatively new to shell programming and would like to know if there is a simple way to output the results of the sar
command to a csv
file. Using sar > file1.csv
does the job, but it is not properly formatted. All the data is present in one column. I tried this, but it was worse
sar -d -u -w 1 1 | grep -v Average | grep -v Linux | tr -s ' ' ',' | tr -d '\n' > file1.csv
Can anyone give me the right script to store the output of the sar
command in a csv
file. Help will be appreciated.
Report Sar Data Using Start Time (sar -s) When you view historic sar data from the /var/log/sa/saXX file using “sar -f” option, it displays all the sar data for that specific day starting from 12:00 a.m for that day. Using “-s hh:mi:ss” option, you can specify the start time.
sar (System Activity Report) is a system utility command used to collect and report different metrics such us system load, CPU activity, memory ( sar -r ), paging ( sar -B ), swap ( sar -S ), disk (sar -d), device load and network. It is extremely useful in analyzing current and recent recorded system performance.
The name "sar" stands for "system activity report," and it can display current performance, provide reports that are based on log files stored in your system's /var/log/sa (or /var/log/sysstat) folder, or be set up to automatically produce daily reports.
Use the sar -r command to report the number of memory pages and swap-file disk blocks that are currently unused. Output from the -r option is described in the table below. The average number of memory pages available to user processes over the intervals sampled by the command. Page size is machine-dependent.
I know this is kind of old but you should, or could, use sadf -dh -- <sar command>
. It is part of the sysstat package and it will give you the csv output without any need for awk
and regex. Actually, the latest versions are also able to output the info to JSON and XML. You can just pick your poison :)
Simple example:
$ sadf -dh -- -p
localhost.localdomain;-1;2014-06-13 08:47:02 UTC;LINUX-RESTART
# hostname;interval;timestamp;CPU;%user;%nice;%system;%iowait;%steal;%idle[...]
localhost.localdomain;600;2014-06-13 09:00:01 UTC;-1;8.80;0.01;1.65;9.51;0.00;80.03
localhost.localdomain;600;2014-06-13 09:10:01 UTC;-1;3.03;0.71;2.41;0.81;0.00;93.05
sar -d -u -w 1 1 | grep -v Average | grep -v Linux |awk '{if ($0 ~ /[0-9]/) { print $1","$2","$4","$5","$6; } }'
22:14:04,CPU,%nice,%system,%iowait
22:14:05,all,0.00,8.53,0.00
22:14:04,proc/s,,,
22:14:05,0.00,,,
22:14:04,DEV,rd_sec/s,wr_sec/s,avgrq-sz
22:14:05,dev8-0,0.00,0.00,0.00
outputs above enjoy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With