Go to the documentation of this file.00001
00002
00003 #include "ace/Monitor_Control/Linux_Network_Interface_Monitor.h"
00004
00005 #if defined (linux) || defined (AIX)
00006
00007 #include "ace/Log_Msg.h"
00008 #include "ace/OS_NS_stdio.h"
00009
00010 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00011
00012 namespace ACE
00013 {
00014 namespace Monitor_Control
00015 {
00016 Linux_Network_Interface_Monitor::Linux_Network_Interface_Monitor (
00017 const char *scan_format)
00018 : value_ (0UL),
00019 start_ (0UL),
00020 scan_format_ (scan_format)
00021 {
00022 this->init ();
00023 }
00024
00025 void
00026 Linux_Network_Interface_Monitor::update_i (void)
00027 {
00028 char buf[1024];
00029 FILE* fp = ACE_OS::fopen (ACE_TEXT ("/proc/net/dev"),
00030 ACE_TEXT ("r"));
00031
00032 if (fp == 0)
00033 {
00034 ACE_ERROR ((LM_ERROR,
00035 ACE_TEXT ("bytes sent - opening ")
00036 ACE_TEXT ("/proc/net/dev failed\n")));
00037 return;
00038 }
00039
00040
00041
00042 void* dummy = ACE_OS::fgets (buf, sizeof (buf), fp);
00043 dummy = ACE_OS::fgets (buf, sizeof (buf), fp);
00044
00045 unsigned long iface_value = 0UL;
00046 ACE_UINT32 iface_index = 0UL;
00047 ACE_UINT64 total_value = 0UL;
00048
00049 while (ACE_OS::fgets (buf, sizeof (buf), fp) != 0)
00050 {
00051 sscanf (buf, this->scan_format_.c_str (), &iface_value);
00052
00053
00054
00055
00056 this->value_array_[iface_index] +=
00057 iface_value
00058 - static_cast<unsigned long> (this->value_array_[iface_index]);
00059 total_value += this->value_array_[iface_index];
00060
00061 ++iface_index;
00062 }
00063
00064 this->value_ = total_value - this->start_;
00065 (void) ACE_OS::fclose (fp);
00066 }
00067
00068 void
00069 Linux_Network_Interface_Monitor::clear_impl (void)
00070 {
00071 this->init ();
00072 }
00073
00074 void
00075 Linux_Network_Interface_Monitor::init (void)
00076 {
00077 for (unsigned long i = 0UL; i < MAX_INTERFACES; ++i)
00078 {
00079 this->value_array_[i] = 0UL;
00080 }
00081
00082
00083
00084
00085 char buf[1024];
00086 FILE* fp = ACE_OS::fopen (ACE_TEXT ("/proc/net/dev"),
00087 ACE_TEXT ("r"));
00088
00089 if (fp == 0)
00090 {
00091 ACE_ERROR ((LM_ERROR,
00092 ACE_TEXT ("bytes sent - opening ")
00093 ACE_TEXT ("/proc/net/dev failed\n")));
00094 return;
00095 }
00096
00097
00098
00099 void* dummy = ACE_OS::fgets (buf, sizeof (buf), fp);
00100 dummy = ACE_OS::fgets (buf, sizeof (buf), fp);
00101
00102 unsigned long iface_value = 0UL;
00103 ACE_UINT32 iface_index = 0UL;
00104
00105 while (ACE_OS::fgets (buf, sizeof (buf), fp) != 0)
00106 {
00107 sscanf (buf, this->scan_format_.c_str (), &iface_value);
00108 this->start_ += iface_value;
00109
00110 ++iface_index;
00111 }
00112
00113 (void) ACE_OS::fclose (fp);
00114 }
00115 }
00116 }
00117
00118 ACE_END_VERSIONED_NAMESPACE_DECL
00119
00120 #endif