[CentOS] parsing out adjacent text

Tue Jun 3 16:10:09 UTC 2014
Stephen Harris <lists at spuddy.org>

On Tue, Jun 03, 2014 at 11:55:55AM -0400, Tim Dunphy wrote:
> while true
> do
> echo "Time and date: $(/bin/date +"%D %H:%M:%S")" >>
> /tmp/apache_request_log >> /tmp/apache_request_log
> echo ???hostname: $(/bin/hostname -f)\n???   >> /tmp/apache_request_log
> echo ???host ip: $(/bin/hostname -i)???   >> /tmp/apache_request_log
> echo "Server Stats: $(/usr/bin/GET `hostname -f`/server-status/?auto |
> /bin/egrep  -i 'kbytes')" >> /tmp/apache_request_log
> echo "Server Stats: $(/usr/bin/GET `hostname -f`/server-status/?auto |
> /bin/egrep  -i 'ReqPerSec')" >> /tmp/apache_request_log
> echo -e "\n"
> sleep 60
> done

Look at this code structure:

  while true
  do
    {
      echo Time and date: $(date +"%D %H:%M:%S")
      echo Hostname: $(hostname -f)
      echo Hostname IP: $(hostname -i)
      ...
      ...
      # Leave two blank lines
      echo
      echo
    } >> /tmp/apache_request_log
    sleep 60
  done

Note how we're only doing one redirect; this makes the code easier
to read and less likely to make a mistake (and more efficient).

> Still can't get the echo -e "\n" statement to print a new line for some
> reason. Other than that I'm good. And thanks for everyone's help!

That's one of the mistakes; you forgot the >> /tmp/apache_request_log
on the echo line.  But "echo" on its own without anything else leaves
a blank line.

The next clever bit is to not call "GET" twice; why make apache do
twice the work?  Call it once and store the results in a variable

  stat=$(GET $(hostname -f)/server-status/?auto)
  echo Server Stats: $(echo "$stat" | grep -i kbytes)
  echo Server Stats: $(echo "$stat" | grep -i ReqPerSec)

(You can get even more clever, but that's a little more involved; we'll
start with some basics :-))

So we end up with something like:

  #!/bin/bash

  # These never change...
  name=$(hostname -f)
  ip=$(hostname -i)

  # Once a minute, record some stats
  while true
  do
    {
      echo Time and date: $(date +"%D %H:%M:%S")
      echo Hostname: $name
      echo Hostname IP: $ip
      stat=$(GET $name/server-status/?auto)
      echo Server Stats: $(echo "$stat" | grep -i kbytes)
      echo Server Stats: $(echo "$stat" | grep -i ReqPerSec)
      echo
      echo
    } >> /tmp/apache_request_log
    sleep 60
  done

-- 

rgds
Stephen