Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sar command value in 24 hour format (from 00:00:00 to 23:59:59) in Linux?

Tags:

linux

time

I want to generate a file which has cpu usage logs in 24 hour format (from 00:00:00 to 23:59:59). I am using sar command but it gives time in AM or PM format which is not required.

Sample:

12:01:01 AM     all      1.33      0.00      1.06      0.24      0.00     97.37
12:02:01 AM     all      1.30      0.00      1.02      0.04      0.00     97.64

Expected result:

00:01:01 all      1.33      0.00      1.06      0.24      0.00     97.37
00:02:01 all      1.30      0.00      1.02      0.04      0.00     97.64
like image 592
Vaibhav Soni Avatar asked Jan 22 '15 15:01

Vaibhav Soni


People also ask

How do I view sar logs in Linux?

These parameters must be written in HH:MM:SS format, or sar will ignore them with an error. The -r argument is used to display memory usage data. 3. sar historic data is stored in /var/log/sa directory in case of RedHat based distributions.

What is sar command in Linux?

sar: System Activity Report. It can be used to monitor Linux system's resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more.

How do you read sar memory?

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.

What is sar output?

Description. The sar command writes to standard output the contents of selected cumulative activity counters in the operating system. The accounting system, based on the values in the count and interval parameters, writes information the specified number of times spaced at the specified intervals in seconds.


1 Answers

The sar program is a so-called locale aware program. Meaning it's output format is controlled by the LC_* environment variables. The variable LC_TIME is responsible for the output of time data. (see man locale).

Looks like you are using a system where LC_TIME is set to en_US - a locale using the 12hours AM/PM format (or LC_TIME was kept empty which will lead to using the default locale). You need to use a locale setting which is using the 24 hour format. Lets say en_UK. You can specify it on the command line:

LC_TIME=en_UK.utf8 sar

Output:

Linux 3.13.0-44-generic (desktop1)  01/22/15    _x86_64_    (4 CPU)

00:00:01        CPU     %user     %nice   %system   %iowait    %steal     %idle
00:02:01        all      2,26      0,00      0,41      0,71      0,00     96,63
00:04:01        all      2,68      0,00      0,72      0,46      0,00     96,13
...

Note! If the locale en_UK.uft8 isn't available on your system, you need to generate it using:

sudo locale-gen en_UK.utf8

or you can use POSIX or C:

LC_TIME='POSIX' sar
LC_TIME='C' sar
like image 171
hek2mgl Avatar answered Oct 05 '22 18:10

hek2mgl