From f80ed24c319ad84048eac0ef175a99b8900488eb Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Fri, 22 Jun 2007 14:51:52 +0200 Subject: added some monitors with output formatted for xmobar darcs-hash:20070622125152-d6583-d613d976577d9380b6390d7a47910148dcdfba56.gz --- Monitors/Mem.hs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Monitors/Mem.hs (limited to 'Monitors/Mem.hs') 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 +-- 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 = + "" ++ + str ++ "" + +mem :: IO String +mem = + do m <- parseMEM + return $ formatMem m + +main :: IO () +main = + do m <- mem + putStrLn m \ No newline at end of file -- cgit v1.2.3