diff options
author | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-06-22 14:51:52 +0200 |
---|---|---|
committer | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-06-22 14:51:52 +0200 |
commit | f80ed24c319ad84048eac0ef175a99b8900488eb (patch) | |
tree | 5263d33803027b6ece46657ee89c1cf6c8d42200 /Monitors/Mem.hs | |
parent | 9159ad58340a6315c8904d58375ecf3ec6180330 (diff) | |
download | xmobar-f80ed24c319ad84048eac0ef175a99b8900488eb.tar.gz xmobar-f80ed24c319ad84048eac0ef175a99b8900488eb.tar.bz2 |
added some monitors with output formatted for xmobar
darcs-hash:20070622125152-d6583-d613d976577d9380b6390d7a47910148dcdfba56.gz
Diffstat (limited to 'Monitors/Mem.hs')
-rw-r--r-- | Monitors/Mem.hs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/Monitors/Mem.hs b/Monitors/Mem.hs new file mode 100644 index 0000000..d8bd601 --- /dev/null +++ b/Monitors/Mem.hs @@ -0,0 +1,99 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Monitors.Mem +-- Copyright : (c) Andrea Rossato +-- License : BSD-style (see LICENSE) +-- +-- Maintainer : Andrea Rossato <andrea.rossato@unibz.it> +-- Stability : unstable +-- Portability : unportable +-- +-- A memory monitor for XMobar +-- +----------------------------------------------------------------------------- + +module Main where + +import Numeric + +data Config = + Config { memNormal :: Integer + , memNormalColor :: String + , memCritical :: Integer + , memCriticalColor :: String + , swapNormal :: Integer + , swapNormalColor :: String + , swapCritical :: Integer + , swapCriticalColor :: String + } + +defaultConfig :: Config +defaultConfig = + Config { memNormal = 80 + , memNormalColor = "#00FF00" + , memCritical = 90 + , memCriticalColor = "#FF0000" + , swapNormal = 15 + , swapNormalColor = "#00FF00" + , swapCritical = 50 + , swapCriticalColor = "#FF0000" + } +config :: Config +config = defaultConfig + +-- Utilities + +takeDigits :: Int -> Float -> Float +takeDigits d n = + read $ showFFloat (Just d) n "" + +floatToPercent :: Float -> String +floatToPercent n = + showFFloat (Just 2) (n*100) "%" + +fileMEM :: IO String +fileMEM = readFile "/proc/meminfo" + +parseMEM :: IO [Float] +parseMEM = + do file <- fileMEM + let content = map words $ take 13 $ lines file + [total, free, buffer, cache,_,_,_,_,_,_,_,swapTotal,swapFree] = map (\line -> (read $ line !! 1 :: Float) / 1024) content + rest = free + buffer + cache + used = total - rest + usedratio = used * 100 / total + swapRatio = 100 - (swapFree / swapTotal * 100) + return [total, free, buffer, cache, rest, used, usedratio, swapFree, swapRatio] + + +formatMem :: [Float] -> String +formatMem [] = "" +formatMem (total:_:buffer:cach:_:used:_:_:swapRatio:_) = + "Ram: " ++ ram ++ " cached: " ++ cache ++ " Swap: " ++ swap + where (memN,memC,swapN,swapC) = (fromIntegral $ memNormal config,fromIntegral $ memCritical config + , fromIntegral $ swapNormal config, fromIntegral $ swapCritical config) + m = floatToPercent ((used + buffer + cach) / total) + sw = show (takeDigits 2 swapRatio) ++ "%" + cache = show (takeDigits 2 cach) ++ "Mb" + ram | (used / total * 100) >= memC = setColor m memCriticalColor + | (used / total * 100) >= memN = setColor m memNormalColor + | otherwise = floatToPercent (used / total) + swap | swapRatio >= swapC = setColor sw swapCriticalColor + | swapRatio >= swapN = setColor sw swapNormalColor + | otherwise = sw +formatMem _ = "" + +setColor :: String -> (Config -> String) -> String +setColor str ty = + "<fc=" ++ ty config ++ ">" ++ + str ++ "</fc>" + +mem :: IO String +mem = + do m <- parseMEM + return $ formatMem m + +main :: IO () +main = + do m <- mem + putStrLn m
\ No newline at end of file |