From: Andrew Lorimer Date: Wed, 27 Jun 2018 10:45:29 +0000 (+1000) Subject: add i3blocks modules X-Git-Url: https://git.lorimer.id.au/scripts.git/diff_plain/aa33e2e9ddb2590ec5f453eff8a5962168e5c4d4 add i3blocks modules --- diff --git a/i3blocks-bandwidth.sh b/i3blocks-bandwidth.sh new file mode 100755 index 0000000..26e5ac7 --- /dev/null +++ b/i3blocks-bandwidth.sh @@ -0,0 +1,110 @@ +#!/bin/bash +# Copyright (C) 2012 Stefan Breunig +# Copyright (C) 2014 kaueraal +# Copyright (C) 2015 Thiago Perrotta + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Get custom IN and OUT labels if provided by command line arguments +while [[ $# -gt 1 ]]; do + key="$1" + case "$key" in + -i|--inlabel) + INLABEL="$2" + shift;; + -o|--outlabel) + OUTLABEL="$2" + shift;; + esac + shift +done + +#[[ -z $INLABEL ]] && INLABEL="IN " +#[[ -z $OUTLABEL ]] && OUTLABEL="OUT " + +# Use the provided interface, otherwise the device used for the default route. +if [[ -n $BLOCK_INSTANCE ]]; then + INTERFACE=$BLOCK_INSTANCE +else + INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }') +fi + +# Issue #36 compliant. +if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || ! [ "`cat /sys/class/net/${INTERFACE}/operstate`" = "up" ] +then + echo "$INTERFACE down" + echo "$INTERFACE down" + echo "#FF0000" + exit 0 +fi + +# path to store the old results in +path="/dev/shm/$(basename $0)-${INTERFACE}" + +# grabbing data for each adapter. +read rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes" +read tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes" + +# get time +time=$(date +%s) + +# write current data if file does not exist. Do not exit, this will cause +# problems if this file is sourced instead of executed as another process. +if ! [[ -f "${path}" ]]; then + echo "${time} ${rx} ${tx}" > "${path}" + chmod 0666 "${path}" +fi + +# read previous state and update data storage +read old < "${path}" +echo "${time} ${rx} ${tx}" > "${path}" + +# parse old data and calc time passed +old=(${old//;/ }) +time_diff=$(( $time - ${old[0]} )) + +# sanity check: has a positive amount of time passed +[[ "${time_diff}" -gt 0 ]] || exit + +# calc bytes transferred, and their rate in byte/s +rx_diff=$(( $rx - ${old[1]} )) +tx_diff=$(( $tx - ${old[2]} )) +rx_rate=$(( $rx_diff / $time_diff )) +tx_rate=$(( $tx_diff / $time_diff )) + +# shift by 10 bytes to get KiB/s. If the value is larger than +# 1024^2 = 1048576, then display MiB/s instead + +# incoming +echo -n "$INLABEL" +rx_kib=$(( $rx_rate >> 10 )) +if hash bc 2>/dev/null && [[ "$rx_rate" -gt 1048576 ]]; then + printf '%sM' "`echo "scale=1; $rx_kib / 1024" | bc`" +else + printf %03d ${rx_kib} # leading zeroes so length is constant + echo -n "K" +fi + +echo -n " " + +# outgoing +echo -n "$OUTLABEL" +tx_kib=$(( $tx_rate >> 10 )) +if hash bc 2>/dev/null && [[ "$tx_rate" -gt 1048576 ]]; then + printf '%sM' "`echo "scale=1; $tx_kib / 1024" | bc`" +else + printf %03d ${tx_kib} # leading zeroes so length is constant + echo -n "K" + +fi diff --git a/i3blocks-cpu.pl b/i3blocks-cpu.pl new file mode 100755 index 0000000..0bff52e --- /dev/null +++ b/i3blocks-cpu.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# +# Copyright 2014 Pierre Mavro +# Copyright 2014 Vivien Didelot +# Copyright 2014 Andreas Guldstrand +# +# Licensed under the terms of the GNU GPL v3, or any later version. + +use strict; +use warnings; +use utf8; +use Getopt::Long; + +# default values +my $t_warn = 50; +my $t_crit = 80; +my $cpu_usage = -1; +my $decimals = 2; + +sub help { + print "Usage: cpu_usage [-w ] [-c ] [-d ]\n"; + print "-w : warning threshold to become yellow\n"; + print "-c : critical threshold to become red\n"; + print "-d : Use decimals for percentage (default is $decimals) \n"; + exit 0; +} + +GetOptions("help|h" => \&help, + "w=i" => \$t_warn, + "c=i" => \$t_crit, + "d=i" => \$decimals, +); + +# Get CPU usage +$ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is +open (MPSTAT, 'mpstat 1 1 |') or die; +while () { + if (/^.*\s+(\d+\.\d+)\s+$/) { + $cpu_usage = 100 - $1; # 100% - %idle + last; + } +} +close(MPSTAT); + +$cpu_usage eq -1 and die 'Can\'t find CPU information'; + +# Print short_text, full_text +printf "%02d.${decimals}%%\n", $cpu_usage; +#printf "%.${decimals}f%%\n", $cpu_usage; + +# Print color, if needed +if ($cpu_usage >= $t_crit) { + print "#FF0000\n"; + exit 33; +} elsif ($cpu_usage >= $t_warn) { + print "#FFFC00\n"; +} + +exit 0; diff --git a/i3blocks-music.pl b/i3blocks-music.pl new file mode 100755 index 0000000..9bee37a --- /dev/null +++ b/i3blocks-music.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +# Requires playerctl binary in $PATH +# If $instance is specified in i3blocks, playerctl will attempt to get data from this player + +use Env qw(BLOCK_INSTANCE); + +my @metadata = (); +my $player_arg = ""; + +if ($ARGV[0] =~ "--player=") { + $player_arg = $ARGV[0]; + $player_arg =~ s/\s+//g; +} + +# Check playback status and set appropriate colour +$_ = qx(playerctl status); +if (not m/Playing/) { + print(""); +} +else { + print(""); +} + +# Obtain & format metadata from playerctl +my $artist = qx(playerctl $player_arg metadata artist); +push(@metadata, $artist) if $artist; + +my $title = qx(playerctl $player_arg metadata title); +$title =~ s/(?:[ ][( ][Ff]eat[. ].*)|(?:[( ][Ff]t[. ].*)//; +push(@metadata, $title) if $title; + +# Print stuff +print(join(" - ", @metadata)) if @metadata; +print("");