├── .gitignore ├── Dockerfile ├── Makefile ├── README.md ├── RPM ├── zabbix-agent-sockstat-0.99-1.el6.src.rpm ├── zabbix-agent-sockstat-0.99-1.el6.x86_64.rpm ├── zabbix-agent-sockstat-2.00-1.el6.src.rpm ├── zabbix-agent-sockstat-2.00-1.el6.x86_64.rpm └── zabbix-agent-sockstat.spec ├── binary_lib └── zbx_sockstat.so ├── img └── zabbix-sockstat.png ├── zabbix_template └── zbx_sockstat.xml └── zbx_sockstat.c /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # container for a fast testing SPEC file 2 | # user and dirs according to Copr Build Service 3 | FROM centos:centos7 4 | ENV LANG=en_US.utf8 5 | RUN yum install gcc make rpm-build -y 6 | RUN useradd -ms /bin/bash worker 7 | RUN mkdir /builddir/ && chown worker: /builddir 8 | USER worker 9 | RUN mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} 10 | COPY zbx_sockstat.c Makefile /home/worker/rpmbuild/SOURCES/ 11 | COPY RPM/zabbix-agent-sockstat.spec /home/worker/rpmbuild/SPECS/ 12 | RUN rpmbuild --undefine=_disable_source_fetch -ba home/worker/rpmbuild/SPECS/zabbix-agent-sockstat.spec 13 | CMD /bin/bash 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | zbx_sockstat: zbx_sockstat.c 2 | gcc -shared -o zbx_sockstat.so zbx_sockstat.c -I../../../include -fPIC 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sockstat-zabbix-template 2 | ===================== 3 | 4 | Description 5 | ----------- 6 | 7 | Zabbix 2.2 comes with support of loadable modules for extending Zabbix agent and server without sacrificing performance. 8 | 9 | A loadable module is basically a shared library used by Zabbix server or agent and loaded on startup. The library should contain certain functions, so that a Zabbix process may detect that the file is indeed a module it can load and work with. 10 | 11 | Loadable modules have a number of benefits. Great performance and ability to implement any logic are very important, but perhaps the most important advantage is the ability to develop, use and share Zabbix modules. It contributes to trouble-free maintenance and helps to deliver new functionality easier and independently of the Zabbix core code base. 12 | 13 | I have created a agent module to parse the */proc/net/sockstat* info for Zabbix > 2.2.x 14 | 15 | You will be able to watch the *orphan* sockets or the *timewait* sockets. They are interesting for: DDOS detection, leaks in webapps services etc etc... 16 | 17 | 18 | Install 19 | ------- 20 | 21 | From **CentOS**: `yum install ` just that. 22 | 23 | From **source**: 24 | 25 | You need to download the Zabbix 2.2.x source and: 26 | 27 | ``` 28 | cd 29 | ./configure 30 | make 31 | 32 | cd /src/modules/ 33 | ``` 34 | 35 | and you should create a new directory with this git repo content. After that, inside of the new module directory, a `make` is enough. 36 | 37 | **Other**: There is a compiled version too. Copy it wherever you want. 38 | 39 | 40 | Configure 41 | --------- 42 | 43 | **CentOS RPM:** Nothing. RPM does it. 44 | 45 | 46 | **No CentOS:** `cd /etc/zabbix/zabbix_agentd.conf` 47 | 48 | ``` 49 | LoadModulePath=/etc/zabbix/modules 50 | LoadModule=zbx_sockstat.so 51 | ``` 52 | 53 | At the moment I only have implemented 5 keys: 54 | 55 | ``` 56 | [root@build zbx_sockstat]# zabbix_agentd -t sockstat.info[total] 57 | sockstat.info[total] [u|991] 58 | [root@build zbx_sockstat]# zabbix_agentd -t sockstat.info[tcp] 59 | sockstat.info[tcp] [u|6] 60 | [root@build zbx_sockstat]# zabbix_agentd -t sockstat.info[orphan] 61 | sockstat.info[orphan] [u|0] 62 | [root@build zbx_sockstat]# zabbix_agentd -t sockstat.info[timewait] 63 | sockstat.info[timewait] [u|135] 64 | [root@build zbx_sockstat]# zabbix_agentd -t sockstat.info[allocated] 65 | sockstat.info[allocated] [u|64] 66 | ``` 67 | I got the info from here: 68 | 69 | ``` 70 | [root@build zbx_sockstat]# cat /proc/net/sockstat 71 | sockets: used 991 72 | TCP: inuse 6 orphan 0 tw 135 alloc 64 mem 3 73 | UDP: inuse 0 mem 1 74 | UDPLITE: inuse 0 75 | RAW: inuse 0 76 | FRAG: inuse 0 memory 0 77 | ``` 78 | 79 | Zabbix 2.2.x template 80 | --------------------- 81 | 82 | There is a xml file (zabbix_template/) to import in Zabbix server with: 83 | 84 | * 5 items 85 | * 1 graph 86 | 87 | Screenshots 88 | ----------- 89 | ![Screenshot](img/zabbix-sockstat.png) 90 | 91 | 92 | Environment 93 | ----------- 94 | 95 | I am using this module with: 96 | 97 | * CentOS 6.5 98 | * Kernel: 2.6.32-431.3.1.el6.x86_64 99 | * Zabbix 2.2.x 100 | 101 | Notes 102 | ----- 103 | 104 | I am not a developer. I am a sysadmin. Sorry for my bad programming practices etc etc ...... 105 | -------------------------------------------------------------------------------- /RPM/zabbix-agent-sockstat-0.99-1.el6.src.rpm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicendominguez/sockstat-zabbix-module/a883c282132ce5240ef34ef555058add1f212b82/RPM/zabbix-agent-sockstat-0.99-1.el6.src.rpm -------------------------------------------------------------------------------- /RPM/zabbix-agent-sockstat-0.99-1.el6.x86_64.rpm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicendominguez/sockstat-zabbix-module/a883c282132ce5240ef34ef555058add1f212b82/RPM/zabbix-agent-sockstat-0.99-1.el6.x86_64.rpm -------------------------------------------------------------------------------- /RPM/zabbix-agent-sockstat-2.00-1.el6.src.rpm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicendominguez/sockstat-zabbix-module/a883c282132ce5240ef34ef555058add1f212b82/RPM/zabbix-agent-sockstat-2.00-1.el6.src.rpm -------------------------------------------------------------------------------- /RPM/zabbix-agent-sockstat-2.00-1.el6.x86_64.rpm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicendominguez/sockstat-zabbix-module/a883c282132ce5240ef34ef555058add1f212b82/RPM/zabbix-agent-sockstat-2.00-1.el6.x86_64.rpm -------------------------------------------------------------------------------- /RPM/zabbix-agent-sockstat.spec: -------------------------------------------------------------------------------- 1 | %define main_version 4.0 2 | %define minor_version 4 3 | %define module_dir src/modules 4 | 5 | Name: zabbix-agent-sockstat 6 | Version: %{main_version}.%{minor_version} 7 | Release: 1%{?dist} 8 | Summary: Agent module for zabbix for network sockets status 9 | 10 | Group: Applications/Internet 11 | License: GPLv2+ 12 | Source1: zbx_sockstat.c 13 | Source2: Makefile 14 | 15 | Requires: zabbix-agent >= 4.0.0, zabbix-agent < 4.1.0 16 | 17 | %if 0%{?rhel} >= 7 18 | BuildRequires: systemd 19 | Requires(post): systemd 20 | Requires(preun): systemd 21 | %else 22 | Requires(preun): /sbin/service 23 | Requires(post): /sbin/service 24 | %endif 25 | 26 | 27 | %description 28 | Agent module to parse the /proc/net/sockstat info for Zabbix > 2.2.x agent. 29 | Info: https://github.com/vicendominguez/sockstat-zabbix-module 30 | 31 | %prep 32 | curl -o /tmp/zbx.rpm https://repo.zabbix.com/zabbix/%{main_version}/rhel/7/SRPMS/zabbix-%{version}-1.el7.src.rpm 33 | rpm -i /tmp/zbx.rpm 34 | %setup -qTcn zabbix-%{version} 35 | tar --strip-components=1 -xf %{_sourcedir}/zabbix-%{version}.tar.gz 36 | echo 'LoadModule=zbx_sockstat.so' > %{_sourcedir}/zabbix-agent-sockstat.conf 37 | 38 | %build 39 | %configure 40 | mkdir -p %{module_dir}/%{name} 41 | cp %{SOURCE1} %{SOURCE2} %{module_dir}/%{name}/ 42 | cd %{module_dir}/%{name}/ 43 | make 44 | 45 | %install 46 | install --directory %{buildroot}%{_libdir}/zabbix/modules/ 47 | install --directory %{buildroot}/etc/zabbix/zabbix_agentd.d 48 | 49 | install -m 0755 %{_builddir}/zabbix-%{version}/%{module_dir}/%{name}/zbx_sockstat.so %{buildroot}%{_libdir}/zabbix/modules/ 50 | install -m 0644 %{_sourcedir}/zabbix-agent-sockstat.conf %{buildroot}/etc/zabbix/zabbix_agentd.d/ 51 | 52 | %clean 53 | rm -rf %{buildroot} 54 | 55 | %files 56 | /%{_libdir}/zabbix/modules/ 57 | /etc/zabbix/zabbix_agentd.d/zabbix-agent-sockstat.conf 58 | 59 | %post 60 | %if 0%{?rhel} >= 7 61 | /usr/bin/systemctl try-restart zabbix-agent.service >/dev/null 2>&1 || : 62 | %else 63 | /sbin/service zabbix-agent restart >/dev/null 2>&1 || : 64 | %endif 65 | 66 | %postun 67 | %if 0%{?rhel} >= 7 68 | /usr/bin/systemctl try-restart zabbix-agent.service >/dev/null 2>&1 || : 69 | %else 70 | /sbin/service zabbix-agent restart >/dev/null 2>&1 || : 71 | %endif 72 | 73 | %changelog 74 | * Fri Feb 8 2019 Anatolii Vorona - 3.4 75 | - Updated spec for EPEL7 and systemd 76 | 77 | * Tue Jan 28 2014 Vicente Dominguez - 2.00 78 | - No malloc, random value solved and first production version 79 | 80 | * Fri Jan 24 2014 Vicente Dominguez - 0.99 81 | - rpm for rhel6 (fast-way) 82 | -------------------------------------------------------------------------------- /binary_lib/zbx_sockstat.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicendominguez/sockstat-zabbix-module/a883c282132ce5240ef34ef555058add1f212b82/binary_lib/zbx_sockstat.so -------------------------------------------------------------------------------- /img/zabbix-sockstat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vicendominguez/sockstat-zabbix-module/a883c282132ce5240ef34ef555058add1f212b82/img/zabbix-sockstat.png -------------------------------------------------------------------------------- /zabbix_template/zbx_sockstat.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2.0 4 | 2014-01-28T11:31:28Z 5 | 6 | 7 | Templates 8 | 9 | 10 | 11 | 241 | 242 | 243 | 244 | Network Sockets Status 245 | 900 246 | 200 247 | 0.0000 248 | 100.0000 249 | 1 250 | 0 251 | 0 252 | 1 253 | 0 254 | 0.0000 255 | 0.0000 256 | 0 257 | 0 258 | 0 259 | 0 260 | 261 | 262 | 0 263 | 0 264 | 00C8C8 265 | 0 266 | 2 267 | 0 268 | 269 | Template Linux Socket Status 270 | sockstat.info[allocated] 271 | 272 | 273 | 274 | 1 275 | 0 276 | 0000C8 277 | 0 278 | 2 279 | 0 280 | 281 | Template Linux Socket Status 282 | sockstat.info[orphan] 283 | 284 | 285 | 286 | 2 287 | 0 288 | 00C800 289 | 0 290 | 2 291 | 0 292 | 293 | Template Linux Socket Status 294 | sockstat.info[tcp] 295 | 296 | 297 | 298 | 3 299 | 0 300 | C800C8 301 | 0 302 | 2 303 | 0 304 | 305 | Template Linux Socket Status 306 | sockstat.info[timewait] 307 | 308 | 309 | 310 | 4 311 | 0 312 | C80000 313 | 0 314 | 2 315 | 0 316 | 317 | Template Linux Socket Status 318 | sockstat.info[total] 319 | 320 | 321 | 322 | 323 | 324 | 325 | -------------------------------------------------------------------------------- /zbx_sockstat.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** This program is free software; you can redistribute it and/or modify 3 | ** it under the terms of the GNU General Public License as published by 4 | ** the Free Software Foundation; either version 2 of the License, or 5 | ** (at your option) any later version. 6 | ** 7 | ** This program is distributed in the hope that it will be useful, 8 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | ** GNU General Public License for more details. 11 | ** 12 | ** You should have received a copy of the GNU General Public License 13 | ** along with this program; if not, write to the Free Software 14 | ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 15 | ** MA 02110-1301, USA. 16 | **/ 17 | 18 | #include "sysinc.h" 19 | #include "module.h" 20 | 21 | /* the variable keeps timeout setting for item processing */ 22 | static int item_timeout = 0; 23 | 24 | int zbx_module_sockstat_info(AGENT_REQUEST *request, AGENT_RESULT *result); 25 | 26 | static ZBX_METRIC keys[] = 27 | /* KEY FLAG FUNCTION TEST PARAMETERS */ 28 | { 29 | {"sockstat.info", CF_HAVEPARAMS, zbx_module_sockstat_info, "total"}, 30 | {NULL} 31 | }; 32 | 33 | /**************************** 34 | * sockstat file parser * 35 | ****************************/ 36 | 37 | int * get_values_sockstat(FILE * fd) { 38 | 39 | static int buffer[5]; 40 | char line[128]; 41 | 42 | if (!fd) 43 | return NULL; 44 | 45 | while (NULL != fgets(line, sizeof(line), fd)) { 46 | 47 | if (0 == strncmp(line,"sockets",7)) { 48 | /* 49 | /proc/net/sockstat format: kernel 2.6.32-431.3.1.el6.x86_64 Centos 6.5 50 | sockets: used 290 51 | */ 52 | sscanf (line, "%*s used %d", &buffer[0]); 53 | } 54 | if (0 == strncmp(line,"TCP",3)){ 55 | /* 56 | /proc/net/sockstat format: kernel 2.6.32-431.3.1.el6.x86_64 Centos 6.5 57 | TCP: inuse 117 orphan 3 tw 669 alloc 121 mem 132 58 | */ 59 | sscanf(line,"%*s inuse %d orphan %d tw %d alloc %d",&buffer[1],&buffer[2],&buffer[3],&buffer[4]); 60 | } 61 | } 62 | return buffer; 63 | } 64 | 65 | /****************************************************************************** 66 | * * 67 | * Function: zbx_module_api_version * 68 | * * 69 | * Purpose: returns version number of the module interface * 70 | * * 71 | * Return value: ZBX_MODULE_API_VERSION_ONE - the only version supported by * 72 | * Zabbix currently * 73 | * * 74 | ******************************************************************************/ 75 | int zbx_module_api_version() 76 | { 77 | return ZBX_MODULE_API_VERSION_ONE; 78 | } 79 | 80 | /****************************************************************************** 81 | * * 82 | * Function: zbx_module_item_timeout * 83 | * * 84 | * Purpose: set timeout value for processing of items * 85 | * * 86 | * Parameters: timeout - timeout in seconds, 0 - no timeout set * 87 | * * 88 | ******************************************************************************/ 89 | void zbx_module_item_timeout(int timeout) 90 | { 91 | item_timeout = timeout; 92 | } 93 | 94 | /****************************************************************************** 95 | * * 96 | * Function: zbx_module_item_list * 97 | * * 98 | * Purpose: returns list of item keys supported by the module * 99 | * * 100 | * Return value: list of item keys * 101 | * * 102 | ******************************************************************************/ 103 | ZBX_METRIC *zbx_module_item_list() 104 | { 105 | return keys; 106 | } 107 | 108 | 109 | /****************************************************************************** 110 | * * 111 | * Function: zbx_module_sockstat_info * 112 | * * 113 | * Purpose: returns values of sockstat proc file * 114 | * * 115 | * Return value: list of values * 116 | * * 117 | ******************************************************************************/ 118 | 119 | int zbx_module_sockstat_info(AGENT_REQUEST *request, AGENT_RESULT *result) 120 | { 121 | char *param; 122 | 123 | char P_PROC_SOCKSTAT[] = "/proc/net/sockstat"; 124 | static FILE *f_sockstat; 125 | int *p_sockstat; 126 | 127 | int j; 128 | int i_out; 129 | 130 | const int N_COMMANDS = 5; 131 | char *s_commands[5] = {"total","tcp","orphan","timewait","allocated"}; 132 | 133 | /* real start */ 134 | 135 | if (1 != request->nparam) 136 | { 137 | /* set optional error message */ 138 | SET_MSG_RESULT(result, strdup("Invalid number of parameters")); 139 | return SYSINFO_RET_FAIL; 140 | } 141 | 142 | param = get_rparam(request, 0); 143 | 144 | f_sockstat = fopen(P_PROC_SOCKSTAT, "r"); 145 | if (0x0 == f_sockstat) { 146 | SET_MSG_RESULT(result, strdup("sockstat not found in /proc")); 147 | return SYSINFO_RET_FAIL; 148 | } 149 | 150 | p_sockstat = get_values_sockstat(f_sockstat); 151 | 152 | i_out = p_sockstat[0]; 153 | for (j=0;j<=N_COMMANDS;j++){ 154 | if (!strcmp(s_commands[j],param)) { 155 | i_out = p_sockstat[j]; 156 | break; 157 | } 158 | } 159 | 160 | fclose (f_sockstat); 161 | SET_UI64_RESULT(result, i_out); 162 | return SYSINFO_RET_OK; 163 | } 164 | 165 | 166 | /****************************************************************************** 167 | * * 168 | * Function: zbx_module_init * 169 | * * 170 | * Purpose: the function is called on agent startup * 171 | * It should be used to call any initialization routines * 172 | * * 173 | * Return value: ZBX_MODULE_OK - success * 174 | * ZBX_MODULE_FAIL - module initialization failed * 175 | * * 176 | * Comment: the module won't be loaded in case of ZBX_MODULE_FAIL * 177 | * * 178 | ******************************************************************************/ 179 | int zbx_module_init() 180 | { 181 | srand(time(NULL)); 182 | return ZBX_MODULE_OK; 183 | } 184 | 185 | /****************************************************************************** 186 | * * 187 | * Function: zbx_module_uninit * 188 | * * 189 | * Purpose: the function is called on agent shutdown * 190 | * It should be used to cleanup used resources if there are any * 191 | * * 192 | * Return value: ZBX_MODULE_OK - success * 193 | * ZBX_MODULE_FAIL - function failed * 194 | * * 195 | ******************************************************************************/ 196 | int zbx_module_uninit() 197 | { 198 | return ZBX_MODULE_OK; 199 | } 200 | --------------------------------------------------------------------------------