Go to the documentation of this file.00001
00002
00003 #include "ace/Monitor_Control/FreeBSD_Network_Interface_Monitor.h"
00004
00005 #if defined (__FreeBSD__) || defined (__Lynx__)
00006
00007 #include "ace/Log_Msg.h"
00008 #include "ace/OS_NS_stdio.h"
00009
00010 #include <sys/types.h>
00011 #include <sys/socket.h>
00012 #include <sys/sysctl.h>
00013 #include <sys/time.h>
00014 #include <net/if.h>
00015 #include <net/if_mib.h>
00016
00017 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00018
00019 namespace ACE
00020 {
00021 namespace Monitor_Control
00022 {
00023 FreeBSD_Network_Interface_Monitor::FreeBSD_Network_Interface_Monitor (
00024 const ACE_TCHAR *lookup_str)
00025 : value_ (0UL),
00026 start_ (0UL),
00027 lookup_str_ (lookup_str)
00028 {
00029 this->init();
00030 }
00031
00032 void
00033 FreeBSD_Network_Interface_Monitor::update_i (void)
00034 {
00035 this->fetch(this->value_);
00036 this->value_ -= this->start_;
00037 }
00038
00039 void
00040 FreeBSD_Network_Interface_Monitor::clear_impl (void)
00041 {
00042 this->init();
00043 }
00044
00045 void
00046 FreeBSD_Network_Interface_Monitor::init (void)
00047 {
00048 this->fetch(this->start_);
00049 this->value_ = 0UL;
00050 }
00051
00052 void
00053 FreeBSD_Network_Interface_Monitor::fetch (ACE_UINT64& value) const
00054 {
00055 ACE_UINT64 count = 0;
00056
00057 int req_name[5];
00058 int ifcount;
00059 size_t ifcount_len = sizeof(ifcount);
00060
00061 req_name[0] = CTL_NET;
00062 req_name[1] = PF_LINK;
00063 req_name[2] = NETLINK_GENERIC;
00064 req_name[3] = IFMIB_SYSTEM;
00065 req_name[4] = IFMIB_IFCOUNT;
00066
00067 if (::sysctl(req_name, 5, &ifcount, &ifcount_len, (void *)0, 0) == -1)
00068 {
00069 ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) %p\n"),
00070 ACE_TEXT("sysctl failed")));
00071
00072 return;
00073 }
00074
00075 for (int i = 1; i <= ifcount; i++)
00076 {
00077 int name[6];
00078 struct ifmibdata ifmd;
00079 size_t len = sizeof(ifmd);
00080
00081 name[0] = CTL_NET;
00082 name[1] = PF_LINK;
00083 name[2] = NETLINK_GENERIC;
00084 name[3] = IFMIB_IFDATA;
00085 name[4] = i;
00086 name[5] = IFDATA_GENERAL;
00087
00088 if(::sysctl(name, 6, &ifmd, &len, (void *)0, 0) == -1)
00089 {
00090 ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) %p\n"),
00091 ACE_TEXT("sysctl failed")));
00092
00093 break;
00094 }
00095
00096 struct if_data * const ifi = &ifmd.ifmd_data;
00097
00098 if(this->lookup_str_ == "ibytes")
00099 {
00100 count += ifi->ifi_ibytes;
00101 }
00102 else if(this->lookup_str_ == "ipackets")
00103 {
00104 count += ifi->ifi_ipackets;
00105 }
00106 else if(this->lookup_str_ == "obytes")
00107 {
00108 count += ifi->ifi_obytes;
00109 }
00110 else if(this->lookup_str_ == "opackets")
00111 {
00112 count += ifi->ifi_opackets;
00113 }
00114
00115 }
00116
00117 value = count;
00118 }
00119 }
00120 }
00121
00122 ACE_END_VERSIONED_NAMESPACE_DECL
00123
00124 #endif