Question

I have been trying to produce an RRD graph through PHP. So far it works fine apart from the last line that I want to have the last update date/time. I have been searching online, but I can not find relevant information to my problem. I found a script written in Bash RRDTool GPRINT formatting with printf that is close to what I want to produce, but the solution can not be applied to my problem. Sample of my code in PHP producing the date is provided below:

date_default_timezone_set('Europe/Stockholm');
$timezone = new DateTime(NULL, new DateTimeZone('Europe/Stockholm'));
$date = date('l jS \of F Y h:i:s A' , $timezone->format('U'));

The output with echo is:

Monday 5th of May 2014 12:40:14 PM

At the end of my graph I am trying to import it as a comment to the graph, sample of code:

"COMMENT: RRD last updated: ".$date."\\n",

The error that I am getting:

<b>Graph error: </b>I don't understand ': Monday 5th of May 2014 12:43:17 PM\n'
in command: 'COMMENT: RRD last updated: Monday 5th of May 2014 12:43:17 PM\n'.

Where I tried to apply \ (backslash) on the character : and the error is transferred on the time as expected:

<b>Graph error: </b>I don't understand ':45:31 PM\n' in command: 'COMMENT: RRD
last updated\: Monday 5th of May 2014 12:45:31 PM\n'.

I tried to apply the same solution on the date syntax:

$date = date('l jS \of F Y h\:i\:s A' , $timezone->format('U'));

But the output error is still the same:

<b>Graph error: </b>I don't understand ':47:48 PM\n' in command: 'COMMENT: RRD
last updated\: Monday 5th of May 2014 12:47:48 PM\n'.

I also tried to import the date syntax inside the COMMENT:

"COMMENT: Graph last updated\: date('l jS \of F Y h\:i\:s A' ,
$timezone->format('U'))\\n"

The error that I get:

PHP Notice:  Undefined property: DateTime::$format

Where at this point I run out of ideas and possible solutions. I am wondering if someone else had similar problem or possible idea of how to solve this problem. I have created a sample of code in Perl doing the same thing and by default Perl prints the date at the bottom. So it should be away to do it.

Thanks in advance for your time and effort to assist me with my problem.

Was it helpful?

Solution

It is necessary to escape all colons within a COMMENT directive, otherwise you receive an error, as you have experienced.

So, if you want this comment, you need to escape them all. A call to str_replace can handle that:

$date = date('l jS \of F Y h:i:s A' , $timezone->format('U'));
$comment = "RRD last updated: ".$date;
$comment = str_replace( ":", "\\:", $comment );
$result = rrd_graph($rrdfile,  array(
.... things go in here ....
  "COMMENT:".$comment."\\n";
));

Of course, you can always do it in the date command, remembering that you need to escape the escapes, though in this case you need to be sure there are no other colons present:

$date = date('l jS \of F Y h\\:i\\:s A' , $timezone->format('U'));
$comment = "RRD last updated \\:".$date."\\n";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top