source: dotfiles/guix/.config/i3blocks/cpu_usage@ eaffc2c

Last change on this file since eaffc2c was 53e5e13, checked in by Mikhail Kirillov <w96k@…>, on Mar 12, 2020 at 7:22:49 PM

Add i3blocks bar for sway

  • Property mode set to 100755
File size: 1.5 KB
Line 
1#!/usr/bin/env perl
2#
3# Copyright 2014 Pierre Mavro <deimos@deimos.fr>
4# Copyright 2014 Vivien Didelot <vivien@didelot.org>
5# Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com>
6#
7# Licensed under the terms of the GNU GPL v3, or any later version.
8
9use strict;
10use warnings;
11use utf8;
12use Getopt::Long;
13
14# default values
15my $t_warn = $ENV{T_WARN} // 50;
16my $t_crit = $ENV{T_CRIT} // 80;
17my $cpu_usage = -1;
18my $decimals = $ENV{DECIMALS} // 2;
19my $label = $ENV{LABEL} // "CPU: ";
20
21sub help {
22 print "Usage: cpu_usage [-w <warning>] [-c <critical>] [-d <decimals>]\n";
23 print "-w <percent>: warning threshold to become yellow\n";
24 print "-c <percent>: critical threshold to become red\n";
25 print "-d <decimals>: Use <decimals> decimals for percentage (default is $decimals) \n";
26 exit 0;
27}
28
29GetOptions("help|h" => \&help,
30 "w=i" => \$t_warn,
31 "c=i" => \$t_crit,
32 "d=i" => \$decimals,
33);
34
35# Get CPU usage
36$ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is
37open (MPSTAT, 'mpstat 1 1 |') or die;
38while (<MPSTAT>) {
39 if (/^.*\s+(\d+\.\d+)[\s\x00]?$/) {
40 $cpu_usage = 100 - $1; # 100% - %idle
41 last;
42 }
43}
44close(MPSTAT);
45
46$cpu_usage eq -1 and die 'Can\'t find CPU information';
47
48# Print short_text, full_text
49print "${label}";
50printf "%.${decimals}f%%\n", $cpu_usage;
51print "${label}";
52printf "%.${decimals}f%%\n", $cpu_usage;
53
54# Print color, if needed
55if ($cpu_usage >= $t_crit) {
56 print "#FF0000\n";
57 exit 33;
58} elsif ($cpu_usage >= $t_warn) {
59 print "#FFFC00\n";
60}
61
62exit 0;
Note: See TracBrowser for help on using the repository browser.