collectd/collectd — Issue #15: apcups plugin polling issues when non-default interval used
Author: octo
Created: 2012-02-21 19:29:01+00:00
State: open

From: Edwin (Mantis #38)

**Description:** Using collectd 4.10.1-2 (Ubuntu 10.10 Maverick repo)

When changing the collectd interval to 30, *apcups plugin* always fails polling after the initial poll succeeds. This causes the plugin to be paused and stats not collected.

Syslog entries will look something like:

```
Jun 15 14:55:28 codis collectd[2607]: apcups plugin: Writing to the socket failed.
Jun 15 14:55:28 codis collectd[2607]: read-function of plugin `apcups' failed. Will suspend it for 60 seconds.
Jun 15 14:56:58 codis collectd[2607]: apcups plugin: Writing to the socket failed.
Jun 15 14:56:58 codis collectd[2607]: read-function of plugin `apcups' failed. Will suspend it for 60 seconds.
```

**Steps To Reproduce:** Change collectd interval to 30

**Additional Information:** This appears to be due to attempting to reuse the network socket and not handling any failures. Workaround is to open a new socket each time.

Patch attached to close the socket everytime. Cleaner patch should probably attempt to reuse the socket if possible and then open a new one on error.

```patch
--- apcups.c.orig 2011-06-16 09:56:52.231552800 -0400
+++ apcups.c 2011-06-16 09:56:56.690889806 -0400
@@ -323,6 +323,18 @@
return (-1);
}

+ uint16_t packet_size = 0;
+ if (global_sockfd < 0)
+ return (0);
+
+ DEBUG ("Gracefully shutting down socket %i.", global_sockfd);
+
+ /* send EOF sentinel */
+ swrite (global_sockfd, (void *) &packet_size, sizeof (packet_size));
+
+ close (global_sockfd);
+ global_sockfd = -1;
+
return (0);
}

```