~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_ppp.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: plugin_ppp.c 840 2007-09-09 12:17:42Z michael $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_ppp.c $
 
3
 *
 
4
 * plugin for ppp throughput
 
5
 *
 
6
 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This file is part of LCD4Linux.
 
10
 *
 
11
 * LCD4Linux is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2, or (at your option)
 
14
 * any later version.
 
15
 *
 
16
 * LCD4Linux is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
24
 *
 
25
 */
 
26
 
 
27
/* 
 
28
 * exported functions:
 
29
 *
 
30
 * int plugin_init_ppp (void)
 
31
 *  adds ppp() function
 
32
 *
 
33
 */
 
34
 
 
35
#include "config.h"
 
36
 
 
37
#include <stdlib.h>
 
38
#include <stdio.h>
 
39
#include <string.h>
 
40
#include <errno.h>
 
41
#include <sys/ioctl.h>
 
42
 
 
43
#ifdef HAVE_NET_IF_PPP_H
 
44
#include <net/if_ppp.h>
 
45
#else
 
46
#warning if_ppp.h not found. PPP support deactivated.
 
47
#endif
 
48
 
 
49
#include "debug.h"
 
50
#include "plugin.h"
 
51
#include "qprintf.h"
 
52
#include "hash.h"
 
53
 
 
54
#ifdef HAVE_NET_IF_PPP_H
 
55
 
 
56
static HASH PPP;
 
57
 
 
58
static int get_ppp_stats(void)
 
59
{
 
60
    int age;
 
61
    int unit;
 
62
    unsigned ibytes, obytes;
 
63
    static int fd = -2;
 
64
    struct ifpppstatsreq req;
 
65
    char key[16], val[16];
 
66
 
 
67
    /* reread every 10 msec only */
 
68
    age = hash_age(&PPP, NULL);
 
69
    if (age > 0 && age <= 10)
 
70
        return 0;
 
71
 
 
72
    /* open socket only once */
 
73
    if (fd == -2) {
 
74
        fd = socket(AF_INET, SOCK_DGRAM, 0);
 
75
        if (fd == -1) {
 
76
            error("socket() failed: %s", strerror(errno));
 
77
            return -1;
 
78
        }
 
79
    }
 
80
 
 
81
    for (unit = 0; unit < 8; unit++) {
 
82
        memset(&req, 0, sizeof(req));
 
83
        req.stats_ptr = (caddr_t) & req.stats;
 
84
        qprintf(req.ifr__name, sizeof(req.ifr__name), "ppp%d", unit);
 
85
 
 
86
        if (ioctl(fd, SIOCGPPPSTATS, &req) == 0) {
 
87
            ibytes = req.stats.p.ppp_ibytes;
 
88
            obytes = req.stats.p.ppp_obytes;
 
89
        } else {
 
90
            ibytes = obytes = 0;
 
91
        }
 
92
        qprintf(key, sizeof(key), "Rx:%d", unit);
 
93
        qprintf(val, sizeof(val), "%d", ibytes);
 
94
        hash_put_delta(&PPP, key, val);
 
95
        qprintf(key, sizeof(key), "Tx:%d", unit);
 
96
        qprintf(val, sizeof(val), "%d", obytes);
 
97
        hash_put_delta(&PPP, key, val);
 
98
 
 
99
    }
 
100
    return 0;
 
101
}
 
102
 
 
103
 
 
104
static void my_ppp(RESULT * result, RESULT * arg1, RESULT * arg2)
 
105
{
 
106
    double value;
 
107
 
 
108
    if (get_ppp_stats() < 0) {
 
109
        SetResult(&result, R_STRING, "");
 
110
        return;
 
111
    }
 
112
    value = hash_get_delta(&PPP, R2S(arg1), NULL, R2N(arg2));
 
113
    SetResult(&result, R_NUMBER, &value);
 
114
}
 
115
 
 
116
#endif
 
117
 
 
118
 
 
119
int plugin_init_ppp(void)
 
120
{
 
121
    hash_create(&PPP);
 
122
#ifdef HAVE_NET_IF_PPP_H
 
123
    AddFunction("ppp", 2, my_ppp);
 
124
#endif
 
125
    return 0;
 
126
}
 
127
 
 
128
void plugin_exit_ppp(void)
 
129
{
 
130
    hash_destroy(&PPP);
 
131
}