abw_rrd.c 4.03 KB
Newer Older
's avatar
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * Copyright (c) 2006, CESNET
 * All rights reserved.
 *
 * LICENSE TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the company nor the names of its contributors 
 *       may be used to endorse or promote products derived from this 
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COMPANY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 *
 */

's avatar
committed
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

#include "abw_rrd.h"
43
#include "abw_common.h"
's avatar
committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57

extern int debug;

/* TODO: check for overflow */

char *abw_rrd_create_filename(char *hostname, char *interface,
	int port, int parameters_id, char *protocol) {

	static char rrd_filename[MAX_RRD_FILENAME+1];
	char *ch;

	strcpy(rrd_filename, RRD_DIRECTORY);

	if (hostname)
58
		sprintf(rrd_filename+strlen(rrd_filename), "%s", hostname);
's avatar
committed
59
60
61
62
63

	/* Remove leading '/' and replace other '/' with '_' */
	if (interface) {
		ch=rrd_filename+strlen(rrd_filename);
		if (interface[0]=='/')
64
			sprintf(rrd_filename+strlen(rrd_filename), "-%s", interface+1);
's avatar
committed
65
		else
66
			sprintf(rrd_filename+strlen(rrd_filename), "-%s", interface);
's avatar
committed
67
68
69
70
71
72
73
74
		while (*ch) {
			if (*ch=='/')
				*ch='_';
			ch++;
		}
	}

	if (port>=0)
75
76
77
78
		sprintf(rrd_filename+strlen(rrd_filename), "-%d", port);

	/* If we do not use parameters, still append default "1" for
		compatibility with scripts */
's avatar
committed
79
80

	if (parameters_id>0)
81
82
83
		sprintf(rrd_filename+strlen(rrd_filename), "-%d", parameters_id);
	else
		sprintf(rrd_filename+strlen(rrd_filename), "-1");
's avatar
committed
84
85

	if (protocol)
86
87
88
89
		sprintf(rrd_filename+strlen(rrd_filename), "-%s", protocol);

	/* if (rrd_filename[strlen(rrd_filename)-1]=='-')
		rrd_filename[strlen(rrd_filename)-1]='\0'; */
's avatar
committed
90

91
92
93
94
	/* 
	 * If protocol is specified, this is a full RRD filename. If protocol is not
	 * specified, this is a prefix for rrd_graph... scripts.
	 */
's avatar
committed
95

96
97
	if (protocol)
		strcat(rrd_filename, ".rrd");
's avatar
committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

	if ((ch=malloc(strlen(rrd_filename)+1))==NULL) {
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return NULL;
	}
	strcpy(ch, rrd_filename);
	
	return ch;
} /* abw_rrd_create_filename() */

int abw_rrd_create_file(char *filename) {

	struct stat rrd_file_stat;
	char command[MAX_COMMAND+1];

	memset((void *)&rrd_file_stat, 0, (size_t)sizeof(rrd_file_stat));
	
   if (stat(filename, &rrd_file_stat)<0) {
		if (errno!=ENOENT) {
			fprintf(stderr, "%s: stat() failed\n", __func__);
			return -1;
	   }
		if (debug)
			printf("%s: RRD file %s does not exist, it will be created\n",
					 __func__, filename);
		sprintf(command, "%s %s", RRD_CREATE_SCRIPT, filename);
		if (debug)
			printf("%s: system(%s)\n", __func__, command);
		if (system(command)<0) {
			fprintf(stderr, "%s: system(%s) failed\n", __func__, command);
			return -1;
		}
	}
	else {
		if (debug)
			printf("%s: RRD file %s already exists\n", __func__, filename);
	}

	return 0;
} /* abw_rrd_create_file() */