GTK/Gnome/Ubuntu Usage Meter

Queries, errors or glitches regarding Member facilities
Post Reply
Dazzled
Volunteer Site Admin
Posts: 6003
Joined: Mon Nov 13, 2006 1:16 pm
Location: Sydney

GTK/Gnome/Ubuntu Usage Meter

Post by Dazzled » Sat Jun 20, 2009 5:09 pm

Edit: 28 Sep 2009 - There is a simplified version posted lower down in this thread.

I once mentioned that the Exetel ADSL usage meter can be scripted, and got nagged to hand out an example ....
Edit: 10 Dec 2009 - Rest of post deleted, as superseded by a later version.
Last edited by Dazzled on Thu Dec 10, 2009 8:40 pm, edited 4 times in total.

Gidget
Volunteer Site Admin
Posts: 1813
Joined: Wed Jan 28, 2004 4:33 am
Location: Sydney

Re: GTK/Gnome/Ubuntu Usage Meter

Post by Gidget » Sun Jun 28, 2009 11:31 am

Thanks, Dazzled :D It's great that customers keep expanding the range of Usage Meters for Exetel users.

I'll sticky this thread so it doesn't get "buried".

Cheers

Gidget
Log a fault ticket here
or call Exetel VOIP numbers (02) 8030 1000 or 1300 788 141 (log faults 24x7)
Exetel Support Portal

Dazzled
Volunteer Site Admin
Posts: 6003
Joined: Mon Nov 13, 2006 1:16 pm
Location: Sydney

Re: GTK/Gnome/Ubuntu Usage Meter

Post by Dazzled » Tue Jun 30, 2009 8:03 am

Oops, my carelessness. Please note that exemeter.sh listed above has had a correction made:

Code: Select all

mthfract () { MFRACT=`echo "scale=2;$(date +%d)/${MONTH[$(date +%m)-1]}" | bc`; }

Dazzled
Volunteer Site Admin
Posts: 6003
Joined: Mon Nov 13, 2006 1:16 pm
Location: Sydney

Re: GTK/Gnome/Ubuntu Usage Meter

Post by Dazzled » Mon Aug 03, 2009 2:01 pm

There is a report of the month "08" (August) being mishandled, so the exemeter.sh script has been amended to strip the leading zero.

Dazzled
Volunteer Site Admin
Posts: 6003
Joined: Mon Nov 13, 2006 1:16 pm
Location: Sydney

Re: GTK/Gnome/Ubuntu Usage Meter

Post by Dazzled » Mon Sep 28, 2009 2:36 pm

Edit: The meter was amended, 10 Oct 2009, below.
Edit: 10 Dec 2009 - Rest of post deleted, as superseded by a later version.

A panel icon if desired (59px x 32px, transparent background)
Attachments
meter2.png
Panel icon
meter2.png (512 Bytes) Viewed 8271 times
Last edited by Dazzled on Thu Dec 10, 2009 8:42 pm, edited 3 times in total.

Dazzled
Volunteer Site Admin
Posts: 6003
Joined: Mon Nov 13, 2006 1:16 pm
Location: Sydney

Re: GTK/Gnome/Ubuntu Usage Meter

Post by Dazzled » Sat Oct 10, 2009 10:54 pm

Second Revision
Third Revision
Fourth Revision
.

1st revision scripts have been amended. See attached code. show-meter.py is unchanged.

Again the only dependency not found in a default Ubuntu system is the powerful xmlstarlet package. It is gettable from the repositories with Synaptic.
Installation Summary:
1. Create a new directory, Exemeter, under your home directory.
2. Start the text editor (usually gedit). In turn:
a) Press "select all" on the forum inserts below, and copy them to the editor.
b) Change the LOGIN variable to your user facility name and password.
c) Save each file to the new directory using the suggested filename.
3. exemeter.sh and show-meter.py need execute permission - use right click->properties in the file manager.
4. At the desktop panel, right click->add to panel. Make a launcher pointed to ~/Exemeter/exemeter.sh.

This still unpolished version includes a wee bit more information, for little extra code, and demonstrates the ease of quickly building GUI applications for Gnome in interpreted scripting, without any need for compiled C or C++ or overblown Java. Even though Python is versatile, and essential here for the callbacks from the GUI, the Bash shell is much quicker to code. The Python could be replaced by its compiled C translation to get gtk.Builder initiated. (Exesms and similar apps can be built like this for GTK widgets in a very short time. Unfortunately shell scripts aren't adaptable to the standard Windows environment)

Possible enhancements:
There is still no "wait" message while the meter is read, but it is usually very brief. A treeview would also be prettier than a textview, but is more complex to set up. The read-then-show-results approach does not permit a re-read button, or a timed poll of Exetel. A gui-wait-read-update design would be needed.

The shell script can be modified to suit any ISP with an online XML meter, so if you do that, please include an acknowledgement, particularly if you re-post it elsewhere.

Some screenshots, to show how the window manager theme is followed in the pop-up.

exemeter.sh

Code: Select all

#!/bin/bash
#-------------------------------------------------------------------
# Display Exetel usage in a GTK popup with peak & offpeak bars.
#
# GUI specified in GTK-Builder XML; needs xmlstarlet to handle XML.
# Revision 4: Dazzled 2009-2010-2011
#-------------------------------------------------------------------

APPHOME="$HOME/Exemeter"
LOGIN="0299991111,PASSWORD"

SOURCEF="https://www.exetel.com.au/members/usagemeter_xml.php?$LOGIN"
TMPF1="$APPHOME/exeusage"
TMPF2="/tmp/exemeter.xml"
XMLGUI="$APPHOME/exemeter.gui"
MONTH=(31 28 31 30 31 30 31 31 30 31 30 31)
barlabel () {
  if [ ! $2 ]; then echo "$1 MB used"
  else echo "$1 MB out of `echo "scale=0;$2/1000" | bc` GB used"; fi }
barfract () {
 if [ ! $2 ] || [ $2 = "0" ]; then echo "0" 
 else FRACT="1"; [ $(echo "$2 > $1" | bc) ] && FRACT=`echo "scale=3;$1/$2" | bc`; echo $FRACT; fi }
 
if [ ! -f $XMLGUI ]; then 
  zenity --error --text="Config not found"; exit 1
  
elif wget --no-check-certificate $SOURCEF -O $TMPF1 2>/dev/null; then
  M=$(date +%m); M=${M/#0/}; (( M-- )) 
  MFRACT=$(echo "scale=2;$(date +%d)/${MONTH[$M]}" | bc)
  
  PKUSE=$(xmlstarlet sel -t -v "//CurrentMonthUsage/PeakDownload"       $TMPF1) 
  PKMAX=$(xmlstarlet sel -t -v "//PlanDetails/PeakTimeDownloadInMB"     $TMPF1)
  OFUSE=$(xmlstarlet sel -t -v "//CurrentMonthUsage/OffpeakDownload"    $TMPF1)
  OFMAX=$(xmlstarlet sel -t -v "//PlanDetails/OffpeakTimeDownloadInMB"  $TMPF1)
  MTIME=$(xmlstarlet sel -t -v "//UpdateDatetime"                       $TMPF1)
  PNAME=$(xmlstarlet sel -t -v "//PlanName"                             $TMPF1)
  BUFF1=$(xmlstarlet sel -T -t -m "//Daily"              \
                            -v UsageDate       -o ": "   \
                            -v PeakDownload    -o " "    \
                            -v OffpeakDownload -n                       $TMPF1)
  BUFF1=$(printf "%12s%10.3f%10.3f\n" $BUFF1)

  if [ -z $OFMAX ] || [ $OFMAX = "0" ] ; then OFFLAB="NO OFF PEAK QUOTA"; else OFFLAB="OFF PEAK";   fi
  if [[ $PNAME == *CV* ]];               then PKUSE=`echo "scale=3;$PKUSE+$OFUSE" | bc`; OFUSE="0"; fi     

  xmlstarlet ed -u "//object[@id='peak_bar']/property[@name='fraction']"    -v $(barfract $PKUSE $PKMAX)      \
                -u "//object[@id='offpeak_bar']/property[@name='fraction']" -v $(barfract $OFUSE $OFMAX)      \
                -u "//object[@id='offpeak_bar']/property[@name='text']"     -v "$OFFLAB"                      \
                -u "//object[@id='month_bar']/property[@name='fraction']"   -v $MFRACT                        \
                -u "//object[@id='peak_text']/property[@name='label']"      -v "$(barlabel $PKUSE $PKMAX)"    \
                -u "//object[@id='offpeak_text']/property[@name='label']"   -v "$(barlabel $OFUSE $OFMAX)"    \
                -u "//object[@id='update_time']/property[@name='label']"    -v "Last updated: $MTIME"         \
                -u "//object[@id='textbuffer1']/property[@name='text']"     -v "$BUFF1"                       \
                -u "//object[@id='plan']/property[@name='label']"           -v "$PNAME downloads this month: Pk, Offpk" \
            $XMLGUI > $TMPF2
            
  $APPHOME/show-meter.py
else 
  zenity --error --text="Exetel site is unreadable"; exit 1 
fi
exit 
exemeter.gui:

Code: Select all

<?xml version="1.0"?>
<!--Dazzled's Exetel usage meter GUI specification Rev 2 -->
<interface>
  <object class="GtkTextBuffer" id="textbuffer1">
    <property name="text">*</property>
  </object>
  <object class="GtkDialog" id="usage_dialog">
    <property name="border_width">5</property>
    <property name="title">Exetel Usage Meter</property>
    <property name="resizable">False</property>
    <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
    <signal handler="on_window_destroy" name="destroy"/>
    <child internal-child="vbox">
      <object class="GtkVBox" id="dialog-vbox6">
        <property name="visible">True</property>
        <property name="spacing">2</property>
        <child>
          <object class="GtkVBox" id="vbox7">
            <property name="visible">True</property>
            <property name="border_width">5</property>
            <child>
              <object class="GtkLabel" id="peak_text">
                <property name="visible">True</property>
                <property name="xalign">0</property>
                <property name="label">Peak Usage</property>
              </object>
            </child>
            <child>
              <object class="GtkProgressBar" id="peak_bar">
                <property name="visible">True</property>
                <property name="text">PEAK</property>
                <property name="ellipsize">PANGO_ELLIPSIZE_START</property>
                <property name="fraction">0</property>
              </object>
              <packing>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkProgressBar" id="offpeak_bar">
                <property name="visible">True</property>
                <property name="text">OFF-PEAK</property>
                <property name="ellipsize">PANGO_ELLIPSIZE_START</property>
                <property name="fraction">0</property>
              </object>
              <packing>
                <property name="position">2</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="offpeak_text">
                <property name="visible">True</property>
                <property name="xalign">0</property>
                <property name="label">Off Peak Usage</property>
              </object>
              <packing>
                <property name="position">3</property>
              </packing>
            </child>
            <child>
              <object class="GtkHSeparator" id="hseparator1">
                <property name="visible">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="position">4</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="warning">
                <property name="visible">True</property>
                <property name="xalign">1</property>
              </object>
              <packing>
                <property name="position">5</property>
              </packing>
            </child>
            <child>
              <object class="GtkProgressBar" id="month_bar">
                <property name="visible">True</property>
                <property name="text">Fraction of Current Month</property>
                <property name="ellipsize">PANGO_ELLIPSIZE_START</property>
                <property name="fraction">0</property>
              </object>
              <packing>
                <property name="position">6</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="update_time">
                <property name="visible">True</property>
                <property name="label">Last Updated:</property>
                <property name="justify">GTK_JUSTIFY_CENTER</property>
                <property name="width-chars">42</property> 
              </object>
              <packing>
                <property name="padding">24</property>
                <property name="position">7</property>
              </packing>
            </child>
            <child>
              <object class="GtkScrolledWindow" id="scrolledwindow1">
                <property name="visible">True</property>
                <property name="vscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
                <property name="shadow-type">GTK_SHADOW_IN</property>
                <child>
                  <object class="GtkTextView" id="textview1">
                    <property name="visible">True</property>
                    <property name="editable">False</property>
                    <property name="buffer">textbuffer1</property>
                  </object>
                </child>
              </object>
              <packing>
                <property name="position">8</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="plan">
                <property name="visible">True</property>
                <property name="label">Plan:</property>
              </object>
              <packing>
                <property name="position">9</property>
              </packing>
            </child>                            
          </object>  
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
        <child internal-child="action_area">
          <object class="GtkHButtonBox" id="dialog-action_area6">
            <property name="visible">True</property>
            <property name="layout_style">GTK_BUTTONBOX_END</property>
            <child>
              <object class="GtkButton" id="cancel_button">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="can_default">True</property>
                <property name="events">GDK_BUTTON_PRESS_MASK | GDK_STRUCTURE_MASK</property>
                <property name="label">gtk-cancel</property>
                <property name="use_stock">True</property>
                <signal handler="on_cancel_button_pressed" name="pressed"/>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="pack_type">GTK_PACK_END</property>
          </packing>
        </child>
      </object>
    </child>
    <action-widgets>
      <action-widget response="-6">cancel_button</action-widget>
    </action-widgets>
  </object>
</interface>
show-meter.py:

Code: Select all

#!/usr/bin/env python
import gtk
gui = "/tmp/exemeter.xml"
  
class Exemeter: 
    def on_window_destroy(self, widget, data=None):        
        gtk.main_quit()
    def on_cancel_button_pressed(self, widget, data=None): 
        gtk.main_quit()
    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file(gui) 
        self.window = builder.get_object("usage_dialog")
        builder.connect_signals(self)
        self.window.show()      
    
if __name__ == "__main__":
    meter = Exemeter()
    gtk.main()
Attachments
Screenshot4.png
Screenshot4.png (29.22 KiB) Viewed 8232 times
Screenshot3.png
Screenshot3.png (29.38 KiB) Viewed 8232 times
Last edited by Dazzled on Wed Apr 14, 2010 8:18 pm, edited 1 time in total.

datman
Posts: 8
Joined: Wed Jan 04, 2006 1:33 pm

Re: GTK/Gnome/Ubuntu Usage Meter

Post by datman » Wed Jan 27, 2010 9:42 pm

Thanks for this!

Dazzled
Volunteer Site Admin
Posts: 6003
Joined: Mon Nov 13, 2006 1:16 pm
Location: Sydney

Re: GTK/Gnome/Ubuntu Usage Meter

Post by Dazzled » Wed Apr 14, 2010 8:23 pm

Third Revision
The meter is now out of date since the new unlimited off-peak plans make a progress bar meaningless for off-peak usage. It is probably overdue to redesign the whole thing, but for the moment the script above has some off-peak fixes, while we wait to see how plans evolve. If you are still on an old plan the meter functions as before.

The file exemeter.sh (above) has been amended. The instructions for installation are unchanged.Don't forget to change the login details to suit your account.

Dazzled
Volunteer Site Admin
Posts: 6003
Joined: Mon Nov 13, 2006 1:16 pm
Location: Sydney

Re: GTK/Gnome/Ubuntu Usage Meter

Post by Dazzled » Tue Mar 29, 2011 11:31 am

Fourth Revision.

An update to the quickie script exemeter.sh (above) to handle the new OCV type plans. Don't forget to change the login details to suit your account. The other 2 files are unchanged.

Changes:
Zero off peak quota is allowed for, and the peak and off peak usage is summed and shown in the peak bar. Unfortunately for the time being the summing depends on a pattern match on the plan name. I hope we will eventually get an element to specifically indicate this.

The fraction of the month bar is still based on a start on the 1st, pending a possible Exetel change of format to suit the new dates some plans have.

Post Reply