File: trafstatsfuncs.sh

package info (click to toggle)
trafstats 0.4.20-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 296 kB
  • ctags: 110
  • sloc: cpp: 1,036; sh: 475; perl: 173; makefile: 98
file content (108 lines) | stat: -rw-r--r-- 2,341 bytes parent folder | download
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# This is a lib file containing helper functions to set
# up (and remove) the trafstats database.
# It expects whatever program called it to have sourced 
# trafstats.conf properly. YOU HAVE BEEN WARNED.


createpgdb() {
        su - postgres -c createdb $DBNAME
        RETURNVAL=$?
        if [ $RETURNVAL != 0 ]
        then
        	return $RETURNVAL
        fi
        # else
        RETURNVAL = createdbtables
        return $RETURNVAL
}

createdbtables() {
        su - postgres -c 'psql trafstats -c "CREATE TABLE rawtraffic (
        source_ip inet NOT NULL,
        source_port int4 DEFAULT 0,
        dest_ip inet NOT NULL,
        dest_port INT4 DEFAULT 0,
        timestamp TIMESTAMP NOT NULL DEFAULT TIMESTAMP(CURRENT_TIME),
        amount int8 NOT NULL
        );"'
        return $?
}

createdbwriter() {
	su - postgres -c createuser -A -D -q $DBUSER
	return $?
}

createdbidentmap() {
	cat >> /etc/postgresql/pg_ident.conf<<EOF 
#Added by trafstats installation#
$DBNAME		root	$DBWRITER
$DBNAME		root	$DBREADER
$DBNAME		postgres	postgres
#End trafstats added section# 
EOF
	RETURNVAL=$?
	if [ $RETURNVAL != 0 ]
	then
		return $RETURNVAL
	fi
	# else
	cat >> /etc/postgresql/pg_hba.conf <<EOF
#Added by trafstats installation#
local		$DBNAME				peer $DBNAME
#End trafstats added section#
EOF
	return $RETURNVAL
}

grantuser() {
	echo -n "Granting write access on rawtraffic in $DBNAME to $DBWRITER..."
	su - postgres -c 'psql $DBNAME -c "GRANT INSERT ON rawtraffic TO $DBWRITER"'
	RETVAL=$?
	if [ $RETVAL == 0 ]
	then
		echo "Done."
	else
		echo "Failed!"
		return $RETVAL
	fi
	
	echo -n "Granting read access on rawtraffic in $DBNAME to $DBREADER..."
	su - postgres -c 'psql $DBNAME -c "GRANT SELECT ON rawtraffic TO $DBREADER"'
	RETVAL=$?
	if [ $RETVAL == 0 ]
	then
		echo "Done."
	else
		echo "Failed!"
	fi
	return $RETVAL
}

removedb() {
	su - postgres -c 'psql template1 -c "DROP DATABASE $DBNAME"'
}

removeusers() { 
	echo -n "Removing reader user: $DBREADER ..."
	su - postgres -c 'psql template1 -c "DROP USER $DBREADER"'
	RETVAL=$?
	if [ $RETVAL == 0 ]
	then
		echo "Done!"
	else
		echo "Failed!"
		return $RETVAL
	fi
	
	echo -n "Removing writer user: $DBWRITER ..."
	su - postgres -c 'psql template1 -c "DROP USER $DBWRITER"'
	if [ $RETVAL == 0 ]
	then
		echo "Done!"
	else
		echo "Failed!"
		return $RETVAL
	fi
}