1#!%TCLSH%
2
3#
4# Display statistics about MAC informations
5#
6# History
7#   2005/05/27 : jean     : design
8#   2010/11/30 : pda/jean : integration a Netmagis
9#   2010/12/12 : pda      : i18n
10#   2010/12/26 : pda      : use cgi-dispatch
11#
12
13#
14# Template pages used by this script
15#
16
17set conf(page)          macstat.html
18
19#
20# Script parameters
21#
22
23set conf(tabresultat) {
24    global {
25        chars {10 normal}
26        align {left}
27        botbar {yes}
28        columns {75 25}
29    }
30    pattern Normal {
31        vbar {yes}
32        column { }
33        vbar {yes}
34        column { }
35        vbar {yes}
36    }
37}
38
39#
40# Netmagis general library
41
42source %LIBNETMAGIS%
43
44# ::webapp::cgidebug ; exit
45
46##############################################################################
47# Utility functions
48##############################################################################
49
50#
51# Compute statistics on addresses
52#
53
54proc stat-addr {dbfd} {
55    global conf
56
57    # list of couples <desc, request>
58    set lcouples {
59	"Number of unique MAC addresses"
60	"SELECT count(DISTINCT ((data).mac)) AS r FROM mac.portmac"
61	"Number of unique IPv4 addresses"
62	"SELECT count(DISTINCT ((data).ip)) AS r FROM mac.ipmac WHERE family((data).ip)=4"
63	"Number of unique IPv6 addresses"
64	"SELECT count(DISTINCT ((data).ip)) AS r FROM mac.ipmac WHERE family((data).ip)=6"
65	"Number of distinct MAC addresses associated with an IPv6 address"
66	"SELECT count(DISTINCT(((data).mac))) AS r FROM mac.ipmac WHERE family((data).ip)=6"
67    }
68
69    set lines {}
70    foreach {desc req} $lcouples {
71	set result ""
72	pg_select $dbfd $req tab {
73	    set result $tab(r)
74	}
75	set desc [mc $desc]
76	lappend lines [list Normal $desc $result]
77    }
78    return [::arrgen::output "html" $conf(tabresultat) $lines]
79}
80
81###############################################################################
82# Display statistics
83###############################################################################
84
85d cgi-register {} {} {
86    global conf
87
88    #
89    # Open MAC database
90    #
91
92    set conninfo [get-conninfo "macdb"]
93    if {[catch {set dbfdmac [pg_connect -conninfo $conninfo]} msg]} then {
94        d error $msg
95    }
96
97    #
98    # Get result
99    #
100
101    set tableau [stat-addr $dbfdmac]
102
103    #
104    # End of script: output page and close database
105    #
106
107    d result $conf(page) [list \
108                                [list %TITLE% [mc "Statistics about MAC/IP addresses"]] \
109                                [list %TABLEAU% $tableau] \
110                            ]
111}
112
113###############################################################################
114# Main procedure
115###############################################################################
116
117d cgi-dispatch "mac" "mac"
118