Non SMF [Paid: $5]: Who here understands shell files ?

Started by MoreBloodWine, December 30, 2011, 09:54:43 PM

Previous topic - Next topic

MoreBloodWine

The below code is something I use on my VPS through a cron to monitor memory and diska usage and the results are emailed to me but I'm curious if it's posible to bold and underline the echo (Current Memory Usage & Current Disk Usage) at the bottom.

Any ideas ?

#!/bin/sh

# Make sure we are running under a VPS system with QoS
if [ ! -f "/proc/user_beancounters" ]; then
        echo "File \"/proc/user_beancounters\" does not exist. Make sure this script runs on a VPS system with QoS
enabled."
        exit $E_NOFILE
fi

# Get memory related variables
let GMEM=`grep vmguarpages /proc/user_beancounters | awk '{print$4}'`/1024*4
let BRST=`grep privvmpages /proc/user_beancounters | awk '{print$5}'`/1024*4
let CMEM=`grep physpages /proc/user_beancounters | awk '{print$2}'`/1024*4
let REQM=`grep privvmpages /proc/user_beancounters | awk '{print$2}'`/1024*4
let FREE=$GMEM-$CMEM

# Calculate free memory
if [ "$FREE" -gt "0" ]; then
        let CMEMP=$CMEM*100/$GMEM
        let FREEP=100-$CMEMP
else
        let CMEMP="100"
        let FREEP="0"
fi

# Calculate pretty output
if [ ${#CMEM} -gt ${#FREE} ]; then
        let LIMIT=${#CMEM}-${#FREE}
        SPACE=''
        while [ $LIMIT -gt 0 ]; do
                SPACE=" $SPACE"
                LIMIT=`expr $LIMIT - 1`
        done
else
        SPACE=''
fi

# Echo results


echo "Current Disk Usage"
df -h
echo ""
echo "Current Memory Usage"
echo "Memory Limit...: $GMEM MB (Burstable: $BRST MB)"
echo "Current Usage..: $CMEM MB (${CMEMP}%) (Requested: $REQM MB)"
echo "Free Memory....: $FREE MB ${SPACE}(${FREEP}%)"


Edit: Changing this to a $5 paid job... payment is made after a successful result assuming what I want to achieve is even possible.
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


MrPhil

Well, depending on what operating system and terminal type you're operating under, presumably you can find the proper "escape codes" to change color, weight, underlining, etc. The codes to bold and underline would usually be a mixture of various control characters (^B, etc.) and data numbers and letters, as would the codes to turn off bold and underline. However, it would depend specifically on what kind of terminal (TTY device?) is being emulated. I'm sure once you know that, you could find the escape codes listed somewhere online.

MoreBloodWine

#2
Quote from: MrPhil on December 31, 2011, 07:12:20 PM
Well, depending on what operating system and terminal type you're operating under, presumably you can find the proper "escape codes" to change color, weight, underlining, etc. The codes to bold and underline would usually be a mixture of various control characters (^B, etc.) and data numbers and letters, as would the codes to turn off bold and underline. However, it would depend specifically on what kind of terminal (TTY device?) is being emulated. I'm sure once you know that, you could find the escape codes listed somewhere online.
All I could tell you is Linux - CENTOS 5.7 x86_64, if you could tell me how to get the info needed to make a proper go at what I'm after then I'd def try and get it (the info).

I don't care what it looks like in PuTTy... I just wana format the two echos for the email I get when the cron runs and send me the data.
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


MrPhil

Ah, so it would have to show up in your email as formatted, not on a terminal? The only way I know of would be to somehow convince cron or whatever to generate emails in HTML format, and then you would simply add <b> and <u> tags to your output. Unfortunately I don't know how to make cron do its email in HTML format. Maybe someone else can tell you, or you can find something online. I'm pretty sure that simply adding HTML tags isn't enough. Let us know if you find a way to do it!

MoreBloodWine

Quote from: MrPhil on December 31, 2011, 10:33:59 PM
Ah, so it would have to show up in your email as formatted, not on a terminal? The only way I know of would be to somehow convince cron or whatever to generate emails in HTML format, and then you would simply add <b> and <u> tags to your output. Unfortunately I don't know how to make cron do its email in HTML format. Maybe someone else can tell you, or you can find something online. I'm pretty sure that simply adding HTML tags isn't enough. Let us know if you find a way to do it!
Nah, I tried HTML tags and all they do is print with the text. I tried looking online but search terms are funny.
Want a sig like mine for your BTCGuild stats ? Then check this out: Spend-ur-Bits


andershz

You probably also need some headers in the mail if you want the HTML tags to be interpreted by the mail client.
MIME-Version: 1.0
Content-type: text/html


I'm not sure you can persuade cron to send these headers, maybe this will work:
http://serverfault.com/questions/102235/get-cron-to-send-html-formatted-emails

If not, you could try to explicitly send the mail from the cron job rather than echoing the output and relying on cron to do it automagically.
Maybe something like this, I haven't tested it.

cat - << END | sendmail -t
to:root@localhost
from:root@localhost
subject:Blah
MIME-Version:1.0
Content-type:text/html

<pre>
<strong><u>Current Disk Usage</u></strong>
$(df -h)

<strong><u>Current Memory Usage</u></strong>
Memory Limit... $GMEM MB (Burstable: $BRST MB)
Current Usage..: $CMEM MB (${CMEMP}%) (Requested: $REQM MB)
Free Memory....: $FREE MB ${SPACE}(${FREEP}%)
</pre>

END


Advertisement: