Extend commit 25680305095bfcedaa46cb017182544183ab743b to the whole cpu object.
[monky] / src / apcupsd.c
index 9d9a29d..7bec156 100644 (file)
@@ -1,4 +1,7 @@
-/* apcupsd.c:  conky module for APC UPS daemon monitoring
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
+ *
+ * apcupsd.c:  conky module for APC UPS daemon monitoring
  *
  * Copyright (C) 2009 Jaromir Smrcek <jaromir.smrcek@zoner.com>
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
- * USA. */
+ * USA.
+ *
+ */
 
 #include "conky.h"
 #include "apcupsd.h"
+#include "logging.h"
 
 #include <errno.h>
+#include <netinet/in.h>
+#include <netdb.h>
 #include <sys/time.h>
 #include <unistd.h>
 
@@ -159,6 +167,11 @@ void update_apcupsd(void) {
                struct hostent* he = 0;
                struct sockaddr_in addr;
                short sz = 0;
+#ifdef HAVE_GETHOSTBYNAME_R
+               struct hostent he_mem;
+               int he_errno;
+               char hostbuff[2048];
+#endif
                //
                // connect to apcupsd daemon
                //
@@ -167,11 +180,19 @@ void update_apcupsd(void) {
                        perror("socket");
                        break;
                }
+#ifdef HAVE_GETHOSTBYNAME_R
+               if (gethostbyname_r(info.apcupsd.host, &he_mem, hostbuff, sizeof(hostbuff), &he, &he_errno) || !he ) {
+                       NORM_ERR("APCUPSD gethostbyname_r: %s", hstrerror(h_errno));
+                       break;
+               }
+#else /* HAVE_GETHOSTBYNAME_R */
                he = gethostbyname(info.apcupsd.host);
                if (!he) {
                        herror("gethostbyname");
                        break;
                }
+#endif /* HAVE_GETHOSTBYNAME_R */
+               
                memset(&addr, 0, sizeof(addr));
                addr.sin_family = AF_INET;
                addr.sin_port = info.apcupsd.port;
@@ -206,15 +227,3 @@ void update_apcupsd(void) {
        memcpy(info.apcupsd.items, apc.items, sizeof(info.apcupsd.items));
        return;
 }
-
-//
-// fills in the N/A strings and default host:port
-//
-void init_apcupsd(void) {
-
-       int i;
-       for (i = 0; i < _APCUPSD_COUNT; ++i)
-               memcpy(info.apcupsd.items[i], "N/A", 4); // including \0
-       memcpy(info.apcupsd.host, "localhost", 10);
-       info.apcupsd.port = htons(3551);
-}