summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Plugins/Monitors/Load/Linux.hs
blob: 19174c78ebcc7fd85fd784248feeb46ee561b846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.Monitors.Load.Linux
-- Copyright   :  Finn Lawler
-- License     :  BSD-style (see LICENSE)
--
-- Author      :  Finn Lawler <flawler@cs.tcd.ie>
-- Maintainer  :  jao <mail@jao.io>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A load average monitor for Xmobar.  Adapted from
-- Xmobar.Plugins.Monitors.Thermal by Juraj Hercek.
--
-----------------------------------------------------------------------------

module Xmobar.Plugins.Monitors.Load.Linux (fetchLoads) where

import Xmobar.Plugins.Monitors.Load.Common (Result(..))
import qualified Data.ByteString.Lazy.Char8 as B
import System.Posix.Files (fileExist)

-- | Parses the contents of a loadavg proc file, returning
-- the list of load averages
parseLoadAvgs :: B.ByteString -> Result
parseLoadAvgs =
  Result . map (read . B.unpack) . take 3 . B.words . head . B.lines

fetchLoads :: IO Result
fetchLoads = do
  let file = "/proc/loadavg"

  exists <- fileExist file
  if exists then
    parseLoadAvgs <$> B.readFile file
    else
    return NA