From 4f4e8de1d8b4d33e02057d085399d25e05d87a17 Mon Sep 17 00:00:00 2001 From: jao Date: Wed, 30 Mar 2022 02:37:00 +0100 Subject: Load: new load average monitor Closes #208 --- changelog.md | 7 ++++++ doc/plugins.org | 16 ++++++++++++- src/Xmobar/Plugins/Monitors.hs | 6 ++++- src/Xmobar/Plugins/Monitors/Load.hs | 45 +++++++++++++++++++++++++++++++++++++ xmobar.cabal | 1 + 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/Xmobar/Plugins/Monitors/Load.hs diff --git a/changelog.md b/changelog.md index daaf121..d5d00d7 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,10 @@ +## Version 0.43 (unreleased) + +_New features_ + + - New monitor `Load` providing load averages (stolen from Finn + Lawler). + ## Version 0.42 (March, 2022) _New features_ diff --git a/doc/plugins.org b/doc/plugins.org index 462256f..81f50f9 100644 --- a/doc/plugins.org +++ b/doc/plugins.org @@ -662,7 +662,21 @@ Run Locks #+end_src -*** Process Monitors +*** Load and Process Monitors +***** =Load Args RefreshRate= + + - Aliases to =load= + + - Args: default monitor arguments. The low and high thresholds + (=-L= and =-H=) refer to load average values. + + - Variables that can be used with the =-t/--template= argument: + =load1=, =load5=, =load15=. + + - Default template: =Load: =. + + - Displays load averages for the last 1, 5 or 15 hours. + ***** =TopProc Args RefreshRate= - Aliases to =top= diff --git a/src/Xmobar/Plugins/Monitors.hs b/src/Xmobar/Plugins/Monitors.hs index 8fd31b7..9a3e3de 100644 --- a/src/Xmobar/Plugins/Monitors.hs +++ b/src/Xmobar/Plugins/Monitors.hs @@ -3,7 +3,7 @@ ----------------------------------------------------------------------------- -- | -- Module : Xmobar.Plugins.Monitors --- Copyright : (c) 2010, 2011, 2012, 2013, 2017, 2018, 2019, 2020 Jose Antonio Ortega Ruiz +-- Copyright : (c) 2010-2013, 2017-2020, 2022 Jose Antonio Ortega Ruiz -- (c) 2007-10 Andrea Rossato -- License : BSD-style (see LICENSE) -- @@ -30,6 +30,7 @@ import Xmobar.Plugins.Monitors.Cpu import Xmobar.Plugins.Monitors.MultiCpu import Xmobar.Plugins.Monitors.Batt import Xmobar.Plugins.Monitors.Bright +import Xmobar.Plugins.Monitors.Load import Xmobar.Plugins.Monitors.Thermal import Xmobar.Plugins.Monitors.ThermalZone import Xmobar.Plugins.Monitors.CpuFreq @@ -65,6 +66,7 @@ data Monitors = Network Interface Args Rate | Battery Args Rate | DiskU DiskSpec Args Rate | DiskIO DiskSpec Args Rate + | Load Args Rate | Thermal Zone Args Rate | ThermalZone ZoneNo Args Rate | Memory Args Rate @@ -124,6 +126,7 @@ instance Exec Monitors where #endif alias (Network i _ _) = i alias (DynNetwork _ _) = "dynnetwork" + alias (Load _ _) = "load" alias (Thermal z _ _) = z alias (ThermalZone z _ _) = "thermal" ++ show z alias (Memory _ _) = "memory" @@ -176,6 +179,7 @@ instance Exec Monitors where start (Thermal z a r) = runM (a ++ [z]) thermalConfig runThermal r start (ThermalZone z a r) = runM (a ++ [show z]) thermalZoneConfig runThermalZone r + start (Load a r) = runM a loadConfig runLoad r start (Memory a r) = runM a memConfig runMem r start (Swap a r) = runM a swapConfig runSwap r start (Battery a r) = runM a battConfig runBatt r diff --git a/src/Xmobar/Plugins/Monitors/Load.hs b/src/Xmobar/Plugins/Monitors/Load.hs new file mode 100644 index 0000000..238d374 --- /dev/null +++ b/src/Xmobar/Plugins/Monitors/Load.hs @@ -0,0 +1,45 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Plugins.Monitors.Load +-- Copyright : Finn Lawler +-- License : BSD-style (see LICENSE) +-- +-- Author : Finn Lawler +-- Maintainer : jao +-- Stability : unstable +-- Portability : unportable +-- +-- A load average monitor for Xmobar. Adapted from +-- Xmobar.Plugins.Monitors.Thermal by Juraj Hercek. +-- +----------------------------------------------------------------------------- + +module Xmobar.Plugins.Monitors.Load (loadConfig, runLoad) where + +import Xmobar.Plugins.Monitors.Common +import qualified Data.ByteString.Lazy.Char8 as B +import System.Posix.Files (fileExist) + +-- | Default configuration. +loadConfig :: IO MConfig +loadConfig = mkMConfig + "Load: " -- template + ["load1", "load5", "load15"] -- available replacements + +-- | Parses the contents of a loadavg proc file, returning +-- the list of load averages +parseLoadAvgs :: B.ByteString -> [Float] +parseLoadAvgs = + map ((read :: String -> Float) . B.unpack) . take 3 . B.words . head . B.lines + +-- | Retrieves load information. Returns the monitor string parsed +-- according to template (either default or user specified). +runLoad :: [String] -> Monitor String +runLoad _ = do + let file = "/proc/loadavg" + exists <- io $ fileExist file + if exists then + (do l <- io $ B.readFile file >>= return . parseLoadAvgs + parseTemplate =<< mapM (showWithColors $ showDigits 2) l) + else + return "Load: N/A" diff --git a/xmobar.cabal b/xmobar.cabal index 8126f53..51cbd14 100644 --- a/xmobar.cabal +++ b/xmobar.cabal @@ -167,6 +167,7 @@ library Xmobar.Plugins.Monitors.CpuFreq, Xmobar.Plugins.Monitors.Disk, Xmobar.Plugins.Monitors.Disk.Common, + Xmobar.Plugins.Monitors.Load, Xmobar.Plugins.Monitors.Mem, Xmobar.Plugins.Monitors.MultiCoreTemp, Xmobar.Plugins.Monitors.MultiCpu, -- cgit v1.2.3