/*
 * RRDMonitor: Statistics with RRDTool
 * Copyright (C) 2003-2004 Luc Saillard <luc_at_saillard_dot_org>
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
#ifndef __RRDMONITOR_H
#define __RRDMONITOR_H

#define RRDTOOL_PATH "/usr/bin/rrdtool"
#define RRDMONITOR_VERSION_STRING	"0.0.6"

#include <sys/types.h>
#include <time.h>
#include "strlib.h"
#include "plugins.h"

struct rrdtool_t
{
  pid_t pid;		/* Pid of rrdtool process */
  int   pipe[2];	/* Pipe use to communicate with rrdtool
			   0 => is use for reading rrdtool stdout
			   1 => is use for writing rrdtool stdin
  			*/
};


extern const char *rrdprefixdir;
extern const char *imgprefixdir;
#define RRDTOOL_INSTANCES	1
extern struct rrdtool_t rrdtool_pipes[RRDTOOL_INSTANCES];

/*
 *
 */

#define DEFAULT_GRAPH_OPTIONS " --alt-y-grid"


/*
 * Some macros to help debuging this program
 */

#ifndef NDEBUG
#define ENABLE_DEBUG_TRACE 1
#define ENABLE_DEBUG 	   1
#else
#define ENABLE_DEBUG_TRACE 0
#define ENABLE_DEBUG 	   0
#endif

/*
 * Some helpes routine to debug, trace the program
 */
#if defined(__GNUC__)
# define GCC_ATTRIBUTE_PRINTF(x,y) __attribute__((__format__(__printf__,x,y)))
# define GCC_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
#else
# define GCC_ATTRIBUTE_PRINTF(x,y) 
# define GCC_ATTRIBUTE_NORETURN
#endif

#if ENABLE_DEBUG_TRACE
# define trace(fmt, args...) real_trace(__PRETTY_FUNCTION__, fmt, ## args)
# define wtrace(buf, count) write(2,buf,count)
  void real_trace(const char *fname, const char *fmt, ... ) GCC_ATTRIBUTE_PRINTF(2,3);
#else
# define trace(fmt, args...) do { } while(0)
# define wtrace(buf,count) do { } while(0)
#endif

#if ENABLE_DEBUG
# define debug(fmt, args...) real_debug(fmt, ## args)
# define wdebug(buf, count) write(2,buf,count)
  void real_debug(const char *fmt, ... ) GCC_ATTRIBUTE_PRINTF(1,2);
#else
# define debug(fmt, args...) do { } while(0)
# define wdebug(buf, count) do { } while(0)
#endif

void error(const char *fmt, ... ) GCC_ATTRIBUTE_PRINTF(1,2);
void die(const char *fmt, ... ) GCC_ATTRIBUTE_PRINTF(1,2) GCC_ATTRIBUTE_NORETURN;

/*
 * Helpers for common functions
 *
 */
const char *pick_color(int i);
void usage(void);
unsigned int hashpjw(const unsigned char *mem, int size);
int generic_get_last_update(const char *rrdfilename, int *rrdpipe, char *last_str);
int check_rrdtool_status(const int *rrdpipe, struct string *str, int do_graph);
int read_from_rrdpipe(const int *rrdpipe, struct string *str);
#define write_to_rrdpipe(rrdpipe,buf,buflen) \
     do { 				\
       write(rrdpipe[1],buf,buflen);	\
       wdebug(buf,buflen); 		\
     } while(0);


/*
 *
 * Mini lib to communicate with rrdtool
 *
 */
char *rrdtool_default_rra(int heartbeat);
void rrdtool_append_default_rra(struct string *str, int heartbeat);
void append_rrdname_with_escape_and_pad(struct string *str, const unsigned char *rrdname, int padding);
void append_rrdtime_as_comment(struct string *str, const unsigned char *timefmt, const struct tm *timecur);
int rrdtool_exec(const char *cmdline);
unsigned int parse_rrdtool_date_spec(const char *str);


#endif

