~m.ch/mysql-server/sigar-plugin

2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
1
#include "splugin.h"
2
3
using namespace std;
4
5
/*
6
  Table data structures and fill functions
7
*/
8
9
static ST_FIELD_INFO os_nwif_schema[]=
10
{
12 by Muslimas Chochlovas
Added error handling -> switching to version 0.5.0; small bug fixes
11
  {"NAME",        16,                  MYSQL_TYPE_VARCHAR,  0, 0, "Name"},
12
  {"TYPE",        64,                  MYSQL_TYPE_VARCHAR,  0, 0, "Type"},
13
  {"DESCRIPTION", 256,                 MYSQL_TYPE_VARCHAR,  0, 0, "Desc"},
14
  {"MAC_ADDRESS", SP_MAC_ADDR_LENGTH,  MYSQL_TYPE_VARCHAR,  0, 0, "Mac_addr"},
15
  {"IP_ADDRESS",  SP_INET_ADDR_LENGTH, MYSQL_TYPE_VARCHAR,  0, 0, "Ip_addr"},
16
  {"DESTINATION", SP_INET_ADDR_LENGTH, MYSQL_TYPE_VARCHAR,  0, 0, "Dest"},
17
  {"BROADCAST",   SP_INET_ADDR_LENGTH, MYSQL_TYPE_VARCHAR,  0, 0, "Broadcast"},
18
  {"NETMASK",     SP_INET_ADDR_LENGTH, MYSQL_TYPE_VARCHAR,  0, 0, "Netmask"},
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
19
  {"FLAGS",       SP_ANY_INT64_LENGTH, MYSQL_TYPE_LONGLONG, 0, MY_I_S_UNSIGNED, 
20
                  "Flags"},
21
  {"MTU",         SP_ANY_INT64_LENGTH, MYSQL_TYPE_LONGLONG, 0, MY_I_S_UNSIGNED, 
22
                  "Mtu"},
23
  {"METRICS",     SP_ANY_INT64_LENGTH, MYSQL_TYPE_LONGLONG, 0, MY_I_S_UNSIGNED, 
24
                  "Metrics"},
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
25
  {0, 0, MYSQL_TYPE_NULL, 0, 0, 0}
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
26
};
27
15 by Muslimas Chochlovas
Added --with-system-type to configure.in; added few comments
28
/*
29
  Transform internet address into human readable form. Address
30
  could be one of the following: MAC, IPv4 or undefined.
31
*/
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
32
static char* inet_addr_to_str(sigar_net_address_t *net_addr)
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
33
{
5 by Muslimas Chochlovas
Changed plugin names (OS_PS->OS_PROCESSES, OS_PS_STAT->OS_PROCESSES_STAT, OS_INFO->OS_INFORMATION, OS_FS->OS_FILESYSTEM, OS_NWIF->OS_NETWORKIF, OS_NWIF_STAT->OS_NETWORKIF_STAT). Fixed function convert_to_in4. Added installation instructions to INSTALL file.
34
  char *A, *B, *C, *D;
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
35
  char *str_addr;
36
  sigar_uint32_t addr= net_addr->addr.in;
37
  unsigned char *mac=  &net_addr->addr.mac[0];
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
38
  
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
39
  switch (net_addr->family){
40
  case SIGAR_AF_INET:
41
    asprintf(&D, SP_UINT32, addr >> 24);
42
    asprintf(&C, SP_UINT32, (addr << 8) >> 24);
43
    asprintf(&B, SP_UINT32, (addr << 16) >> 24);
44
    asprintf(&A, SP_UINT32, (addr << 24) >> 24);
45
    asprintf(&str_addr, "%s.%s.%s.%s", A, B, C, D); 
46
    return str_addr;
47
  case SIGAR_AF_LINK:
48
    asprintf(&str_addr, "%02X:%02X:%02X:%02X:%02X:%02X", (mac[0] & 0xff), 
49
            (mac[1] & 0xff), (mac[2] & 0xff), (mac[3] & 0xff), (mac[4] & 0xff), 
50
            (mac[5] & 0xff)); 
51
    return str_addr;
52
  default:
53
    asprintf(&str_addr, "%s", "UNDEFINED");
54
    return str_addr;
55
  }
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
56
}
57
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
58
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
59
int os_nwif_fill_table(THD *thd, TABLE_LIST *tables, COND *cond)
60
{
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
61
  DBUG_ENTER("nwif_plugin_fill_table");
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
62
  CHARSET_INFO *scs= system_charset_info;
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
63
  TABLE *table=      tables->table;
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
64
  int error=         0;
12 by Muslimas Chochlovas
Added error handling -> switching to version 0.5.0; small bug fixes
65
  SP_RETURN_TYPE return_code;
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
66
  size_t i;
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
67
  char *inet_address;
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
68
  
69
  /* init sigar */
70
  sigar_t *t;
12 by Muslimas Chochlovas
Added error handling -> switching to version 0.5.0; small bug fixes
71
  return_code= sigar_open(&t);
72
  
73
  if (return_code == SIGAR_OK)
74
  {
75
    /* get data on each nw interface */
76
    sigar_net_interface_list_t iflist;
77
    return_code= sigar_net_interface_list_get(t, &iflist);
78
    if (return_code == SIGAR_OK)
79
    {
80
      for (i = 0; i < iflist.number; i++) 
81
      {
82
        sigar_net_interface_config_t *ifconfig;
83
        ifconfig= (sigar_net_interface_config_t*) 
84
                   malloc(sizeof(sigar_net_interface_config_t));
85
        
86
        return_code= sigar_net_interface_config_get(t, iflist.data[i], 
87
                                                    ifconfig);
88
        //inet_address= (char*) malloc(SIGAR_INET6_ADDRSTRLEN);
89
        if (return_code == SIGAR_OK)
90
        {
91
          table->field[0]->store(ifconfig->name, strlen(ifconfig->name), scs);
92
          table->field[1]->store(ifconfig->type, strlen(ifconfig->type), scs);
93
          table->field[2]->store(ifconfig->description, 
94
                                 strlen(ifconfig->description), scs);
95
          /* convert mac && ip addresses to string */
96
          inet_address= inet_addr_to_str(&ifconfig->hwaddr);
97
          table->field[3]->store(inet_address, strlen(inet_address), scs);
98
          
99
          inet_address= inet_addr_to_str(&ifconfig->address);
100
          table->field[4]->store(inet_address, strlen(inet_address), scs);
101
          
102
          inet_address= inet_addr_to_str(&ifconfig->destination);
103
          table->field[5]->store(inet_address, strlen(inet_address), scs);
104
          
105
          inet_address= inet_addr_to_str(&ifconfig->broadcast);
106
          table->field[6]->store(inet_address, strlen(inet_address), scs);
107
          
108
          inet_address= inet_addr_to_str(&ifconfig->netmask);
109
          table->field[7]->store(inet_address, strlen(inet_address), scs);
110
          
111
          table->field[8]->store(ifconfig->flags, SP_UNSIGNED);
112
          table->field[9]->store(ifconfig->mtu, SP_UNSIGNED);
113
          table->field[10]->store(ifconfig->metric, SP_UNSIGNED);
114
        }
115
        else
116
        {
117
          sql_print_warning("OS_NETWORK_INTERFACES: Couldn't fetch data for %s",
118
                            iflist.data[i]);    
119
        }
120
        
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
121
        error= error | schema_table_store_record(thd, table);
12 by Muslimas Chochlovas
Added error handling -> switching to version 0.5.0; small bug fixes
122
        free(ifconfig);
123
      }
124
      sigar_net_interface_list_destroy(t, &iflist);
125
    }
126
    else
127
    {
128
      my_error(ER_SP_FETCH_NO_DATA, MYF(0));
129
    }
130
    /* deinit sigar */
131
    sigar_close(t);
132
  }
133
  else
134
  {
135
    my_error(ER_SP_FETCH_NO_DATA, MYF(0));
136
  }
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
137
  
138
  DBUG_RETURN(error);
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
139
}
140
141
/*
142
  Plugin init and deinit functions 
143
*/
144
145
static int os_nwif_plugin_init(void *p)
146
{
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
147
  DBUG_ENTER("init_nwif_plugin");
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
148
  ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
149
  schema->fields_info= os_nwif_schema;
150
  schema->fill_table= os_nwif_fill_table;
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
151
  
152
  DBUG_RETURN(0);
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
153
}
154
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
155
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
156
static int os_nwif_plugin_deinit(void *p)
157
{
14 by Muslimas Chochlovas
Added DBUG statements, return value to fill_table functions, other small fixes
158
  DBUG_ENTER("deinit_nwif_plugin");
159
  DBUG_RETURN(0);
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
160
}
161
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
162
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
163
struct st_mysql_information_schema os_nwif_plugin=
164
{ MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };
165
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
166
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
167
struct st_mysql_plugin os_nwif= 
168
{
169
  MYSQL_INFORMATION_SCHEMA_PLUGIN,                 
170
  &os_nwif_plugin,                                 
6 by Muslimas Chochlovas
A lot of changes to table columns data types, code fixes and optimization, mac && ip4 conversion function revised
171
  "OS_NETWORK_INTERFACES",                                       
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
172
  SP_AUTHOR,                 
9 by Muslimas Chochlovas
No more memory leaks, sigar structures are allocated and cleaned up, installation instructions in README
173
  "SIGAR Network Interfaces.",                           
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
174
  PLUGIN_LICENSE_GPL,                              
175
  os_nwif_plugin_init,                             
176
  os_nwif_plugin_deinit,                           
9 by Muslimas Chochlovas
No more memory leaks, sigar structures are allocated and cleaned up, installation instructions in README
177
  SP_MAJOR_VERSION | SP_MINOR_VERSION,                                          
2 by Muslimas Chochlovas
Added OS_NWIF && OS_NWIF_STAT plugins, small code fixes
178
  NULL,                                            
179
  NULL,                                            
180
  NULL         
181
};