0

I am using sh(1) shell script to generate RRDTool for day, week, month and year. My question is, how to make this code more effective without repeating the same code? I tried it like this but always get errors, use trailing escape character etc:

NEWVAR="-a PNG -v "Interrupts/s" -h 130 -w 576 \
    --watermark "`hostname`:`date "+%H:%M:%S - %d/%m/%Y"`" --font TITLE:15: \
    --font LEGEND:7: --font UNIT:7: --font AXIS:5: -c CANVAS#000000 -c BACK#D8D8D8 \
    DEF:DI="$RRDFDI":DI:AVERAGE \
    CDEF:DIx=DI,8000,LT,DI,UNKN,IF \
    AREA:DIx#FFDD44:"" \
    LINE1.2:DIx#31B404:"Device Interrupts" \
    GPRINT:DIx:MIN:"Min\:%6.0lf" \
    GPRINT:DIx:AVERAGE:"Avg\:%6.0lf" \
    GPRINT:DIx:MAX:"Max\:%6.0lf" \
    GPRINT:DIx:LAST:"Cur\:%6.0lf\l""

$RRDBIN graph $NEWVAR -t "Device Interrupts Days Statistic (Interrupts)" --start -1d
$RRDBIN graph $NEWVAR -t "Device Interrupts Weeks Statistic (Interrupts)" --start -1w 
$RRDBIN graph $NEWVAR -t "Device Interrupts Months Statistic (Interrupts)" --start -1m 
$RRDBIN graph $NEWVAR -t "Device Interrupts Years Statistic (Interrupts)" --start -1y 

Thank in advance!

Sam
  • 4,694
  • 2
  • 36
  • 47
budsz
  • 21
  • 2

2 Answers2

0

Use a for loop, something along the lines of following.

NEWVAR="-a PNG -v "Interrupts/s" -h 130 -w 576 \
  --watermark "`hostname`:`date "+%H:%M:%S - %d/%m/%Y"`" --font TITLE:15: \
  --font LEGEND:7: --font UNIT:7: --font AXIS:5: -c CANVAS#000000 -c BACK#D8D8D8 \
  DEF:DI="$RRDFDI":DI:AVERAGE \
  CDEF:DIx=DI,8000,LT,DI,UNKN,IF \
  AREA:DIx#FFDD44:"" \
  LINE1.2:DIx#31B404:"Device Interrupts" \
  GPRINT:DIx:MIN:"Min\:%6.0lf" \
  GPRINT:DIx:AVERAGE:"Avg\:%6.0lf" \
  GPRINT:DIx:MAX:"Max\:%6.0lf" \
  GPRINT:DIx:LAST:"Cur\:%6.0lf\l""

for c in d w m y ; do
  $RRDBIN graph $NEWVAR -t "Device Interrupts Days Statistic (Interrupts)" --start -1$c output.file.$c
done
  • But I still got error: Interrupts GPRINT:DIx:MIN:Min:%6.0lf GPRINT:DIx:AVERAGE:Avg:%6.0lf GPRINT:DIx:MAX:Max:%6.0lf GPRINT:DIx:LAST:Cur:%6.0lfl: not found ERROR: missing filename – budsz Feb 15 '14 at 12:12
  • Well, obviously I have no idea what your `$RRDBIN` expects, so you should provide an example of the whole command in your question. Edited the answer to reflect a best guess for what your command might expect. –  Feb 15 '14 at 14:35
  • IMHO, is code with the same lines don't need rewrite again, so I need insert (The same code) to variable. I just need changes -t, IMGPATH, --start options. – budsz Feb 16 '14 at 05:54
0

The most obvious reason for your errors when using this code is that you have failed to escape the embedded double quotes in your declaration of NEWVAR.

You cannot generate all three graphs simulataneously; the way you are coding it is optimal, though you need to fix your use of embedded quotes in the NEWVAR declaration. You will also need to double-escape the colons and \l endof line sequence...

NEWVAR="-a PNG -v \"Interrupts/s\" -h 130 -w 576 \ --watermark \""`hostname`":"`date '+%H:%M:%S - %d/%m/%Y'`"\" --font TITLE:15: \ --font LEGEND:7: --font UNIT:7: --font AXIS:5: -c CANVAS#000000 -c BACK#D8D8D8 \
DEF:DI=$RRDFDI:DI:AVERAGE \
CDEF:DIx=DI,8000,LT,DI,UNKN,IF \ AREA:DIx#FFDD44:\"\" \
LINE1.2:DIx#31B404:'Device Interrupts' \
GPRINT:DIx:MIN:'Min\:%6.0lf' \ GPRINT:DIx:AVERAGE:'Avg\:%6.0lf' \
GPRINT:DIx:MAX:'Max\:%6.0lf' \ GPRINT:DIx:LAST:'Cur\:%6.0lf\l'"

Steve Shipway
  • 3,754
  • 3
  • 22
  • 39