Mercenaire
Mercenaire

Reputation: 206

CPU and HDD information

I searched but I found nothing for my problem. I would like to have in Linux command line the information about the CPU usage and the local HDDs with formatting text like exactly as the examples below for my program. These examples are command line outputs on MS-Windows. I hope it is possible on Linux, too.

Thank you

wmic logicaldisk where drivetype=3 get caption,freespace,size

Caption  FreeSpace     Size
C:       135314194432  255953203200
D:       126288519168  128033222656
E:       336546639872  1000194015232
F:       162184503296  1000194015232

wmic cpu get loadpercentage

LoadPercentage
4

Upvotes: 1

Views: 145

Answers (2)

shaola
shaola

Reputation: 18

There is no command that gives you a load percentage of the cpu. It's actually impossible to get that with a system call (nor in linux neither in Windows). What you can get is the number of ticks currently executed (for each field, user, system, io,irq idle)..., then call it again a certain amount of time later and calculate it. That way is how work all the commands for reading the cpu percentage.

Here a script bash that gives you that: (just create a file named for example cpu.sh paste this code and execute to see the results)

_estado()
{
      cat /proc/stat | grep "cpu " |  sed -e 's/  */:/g' -e 's/^cpux//'
}

_ticksconcretos()
{
      echo $1 | cut -d ':' -f $2 

}

while true ; do
      INICIAL=$(_estado)
      sleep 1
      FINAL=$(_estado)

      UsuarioI=$(_ticksconcretos $INICIAL 1)
      UsuarioF=$(_ticksconcretos $FINAL 1)

      NiceI=$(_ticksconcretos $INICIAL 2)
      NiceF=$(_ticksconcretos $FINAL 2)

      SistemaI=$(_ticksconcretos $INICIAL 3)
      SistemaF=$(_ticksconcretos $FINAL 3)

      idleI=$(_ticksconcretos $INICIAL 4)
      idleF=$(_ticksconcretos $FINAL 4) 

      IOI=$(_ticksconcretos $INICIAL 5)
      IOF=$(_ticksconcretos $FINAL 5)

      IRQI=$(_ticksconcretos $INICIAL 6)
      IRQF=$(_ticksconcretos $FINAL 6)

      SOFTIRQI=$(_ticksconcretos $INICIAL 7)
      SOFTIRQF=$(_ticksconcretos $FINAL 7)   

      STEALI=$(_ticksconcretos $INICIAL 8)
      STEALF=$(_ticksconcretos $FINAL 8)   

      InactivoF=$(( $idleF + $IOF ))
      InactivoI=$(( $idleI + $IOI ))
      ActivoI=$(( $UsuarioI + $NiceI + $SistemaI + $IRQI + $SOFTIRQI + $STEALI ))
      ActivoF=$(( $UsuarioF + $NiceF + $SistemaF + $IRQF + $SOFTIRQF + $STEALF ))

      TOTALI=$(( $ActivoI + $InactivoI ))
      TOTALF=$(( $ActivoF + $InactivoF ))

      PORC=$(( ( ( ( $TOTALF - $TOTALI ) - ( $InactivoF - $InactivoI ) ) * 100 / ( $TOTALF - $TOTALI ) ) ))
      clear
      echo "CPU:  $PORC %"

      done

For the free space You could use something like this:

df -h  -x tmpfs -x devtmpfs | awk -F " " '{print $1 " " $4 " " $2}'

wich will give you this output:

Filesystem Free Size
/dev/sda1 16G 25G
/dev/sda5 46G 79G
/dev/sdb8 130G 423G

sda represents the first disk, sda1 the first partition, sda2, the second one etc. you can add (or change) $6 inside the print to get the mount points instead of the partitions, change the order or even more things.

Upvotes: 0

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27496

You won't find anything exactly like the output you provided. The only option is to use for disk space df:

Filesystem                   1K-blocks      Used Available Use% Mounted on
/dev/root                     73216256  27988724  41485276  41% /
devtmpfs                       8170164         0   8170164   0% /dev
tmpfs                          8203680       544   8203136   1% /dev/shm
tmpfs                          8203680     12004   8191676   1% /run
tmpfs                             5120         4      5116   1% /run/lock
tmpfs                          8203680         0   8203680   0% /sys/fs/cgroup
/dev/sdb1                       482922     83939    374049  19% /boot

and for cpu you have many more options, e.g. vmstat

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 11865304 149956 1474172    0    0    53    46  126  707  3  0 96  0  0

or top -b | head:

top - 21:48:43 up 54 min,  1 user,  load average: 0.13, 0.17, 0.22
Tasks: 188 total,   1 running, 187 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.0 us,  0.4 sy,  0.1 ni, 96.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16407364 total, 11848936 free,  2888844 used,  1669584 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 13230972 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0   40544   6440   3780 S   0.0  0.0   0:01.15 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/0

Upvotes: 3

Related Questions