[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apcupsd-users
Subject:    Re: [Apcupsd-users] Install divots
From:       Adam Kropelin <adam () kroptech ! com>
Date:       2014-03-28 2:34:50
Message-ID: CADAPGLpNt6dKkXrjUzvxUZOTqFbjoE2NwW0Ju9sWZu8=1koYLA () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Tue, Mar 25, 2014 at 9:39 AM, Adam Kropelin <adam@kroptech.com> wrote:

> On Sat, Mar 22, 2014 at 8:23 PM, Timothe Litt <litt@acm.org> wrote:
>
>> I also have noted that apcaccess doesn't read the apcupsd.conf file for
>> its default; it uses localhost.  So if one specifies an IP address for
>> NSIP, one always has to specify it to run apcaccess.  I think it's more
>> friendly for the default to be the value of NISIP:NISPORT from
>> apcupsd.conf... That way the default is always to talk to the local
>> apcupsd.  A new patch follows that implements this.  (If apcupsd.conf isn't
>> readable, it silently uses the old default.)
>
>
> Thanks, I'll take a look...
>

...and I finally did.

I took the idea, and most of your code, and modified it slightly.

Used getopt() for proper argument parsing.
Used '-f' to specify conf file for solidarity with apcupsd itself.
Made failure to find explicitly specified conf file fatal.
General cleanup of existing code.

New apcaccess.c attached for your perusal and testing (if you don't
mind...).

--Adam

[Attachment #5 (text/html)]

<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Mar 25, 2014 \
at 9:39 AM, Adam Kropelin <span dir="ltr">&lt;<a href="mailto:adam@kroptech.com" \
target="_blank">adam@kroptech.com</a>&gt;</span> wrote:<br> <blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div \
class="gmail_quote"><div class="">On Sat, Mar 22, 2014 at 8:23 PM, Timothe Litt <span \
dir="ltr">&lt;<a href="mailto:litt@acm.org" \
target="_blank">litt@acm.org</a>&gt;</span> wrote:<br> </div><div><div \
class=""><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid \
rgb(204,204,204);padding-left:1ex" class="gmail_quote">I also have noted that \
apcaccess doesn&#39;t read the apcupsd.conf  file for its default; it uses localhost. \
So if one specifies an  IP address for NSIP, one always has to specify it to run
      apcaccess.  I think it&#39;s more friendly for the default to be the
      value of NISIP:NISPORT from apcupsd.conf... That way the default
      is always to talk to the local apcupsd.  A new patch follows that
      implements this.  (If apcupsd.conf isn&#39;t readable, it silently
      uses the old default.)</blockquote><div><br>
      </div></div><div>Thanks, I&#39;ll take a \
look...</div></div></div></div></div></blockquote><div><br></div><div>...and I \
finally did.</div><div><br></div><div>I took the idea, and most of your code, and \
modified it slightly.</div> <div><br></div><div>Used getopt() for proper argument \
parsing.</div><div>Used &#39;-f&#39; to specify conf file for solidarity with apcupsd \
itself.</div><div>Made failure to find explicitly specified conf file fatal.</div> \
<div>General cleanup of existing code.</div><div><br></div><div>New apcaccess.c \
attached for your perusal and testing (if you don&#39;t \
mind...).</div><div><br></div><div>--Adam</div></div><br></div></div>

--001a11c33b02c827a504f5a18bbd--


["apcaccess.c" (text/x-csrc)]

/*
 * apcaccess.c
 *
 * Text based IPC management tool for apcupsd package.
 */

/*
 * Copyright (C) 2000-2006 Kern Sibbald
 * Copyright (C) 1996-99 Andre M. Hedrick <andre@suse.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include "apc.h"

/* Get and print status from apcupsd NIS server */
static int do_pthreads_status(const char *host, int port, const char *par)
{
   int sockfd, n;
   char recvline[MAXSTRING + 1];

   if ((sockfd = net_open(host, NULL, port)) < 0) {
      fprintf(stderr, "Error contacting apcupsd @ %s:%d: %s\n",
         host, port, strerror(-sockfd));
      return 1;
   }

   net_send(sockfd, "status", 6);

   while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
      recvline[n] = 0;
      if (par) {
        char *line;
        char *r = NULL;
        char *var;

        var = strtok_r(recvline, ":", &r);
        if (!var)
           continue;
        line = recvline + strlen(var) + 1;
        if ((r = strchr( var, ' ' )))
           *r = '\0';
        if (!strcmp(par, var)) {
           while(*line && *line == ' ')
              line++;
           fputs(line, stdout);
           par = NULL;
           break;
        }
        continue;
      }
      fputs(recvline, stdout);
   }

   if (n < 0) {
      fprintf(stderr, "Error reading status from apcupsd @ %s:%d: %s\n",
         host, port, strerror(-n));
      net_close(sockfd);
      return 1;
   }

   net_close(sockfd);
   return par ? 2 : 0;
}

/*********************************************************************/

#if defined(HAVE_MINGW)
#undef main
#endif

void usage()
{
   fprintf(stderr, 
      "usage: apcaccess [-f <config-file>] [-h <host>[:<port>]] "
                       "[-p <pattern>] [<command>] [<host>[:<port>]]\n");
   
}

int main(int argc, char **argv)
{
   const char *par = NULL;
   char *cfgfile = NULL;
   char DEFAULT_HOST[] = "localhost";
   char *host = DEFAULT_HOST;
   const char *cmd = "status";
   int port = NISPORT;
   FILE *cfg;

#ifdef HAVE_MINGW
   WSA_Init();                   /* init MS networking */
#endif

   // Process standard options
   char ch;
   while ((ch = getopt(argc, argv, "f:h:p:")) != -1)
   {
      switch (ch)
      {
      case 'f':
         cfgfile = optarg;
         break;
      case 'h':
         host = optarg;
         break;
      case 'p':
         par = optarg;
         break;
      case '?':
      default:
         usage();
         return 1;
      }
   }

   // Remaining non-option arguments are optional command and host:port
   // These are from legacy apcaccess syntax
   int optleft = argc - optind;
   if (optleft >= 1)
      cmd = argv[optind];
   if (optleft >= 2)
      host = argv[optind + 1];

   // Default cfgfile if not provided on command line
   // Remember if we defaulted so we know later if conf failure is fatal
   bool fatal = cfgfile != NULL;
   if (!cfgfile)
      cfgfile = APCCONF;

   // Parse conf file
   if ((cfg = fopen(cfgfile, "r")))
   {
      fclose(cfg);
      UPSINFO ups;
      memset(&ups, 0, sizeof(UPSINFO));
      init_ups_struct(&ups);
      check_for_config(&ups, cfgfile);
      port = ups.statusport;
      host = ups.nisip;
   }
   else if (fatal)
   {
      // Failure to find explicitly specified conf file is fatal
      fprintf(stderr, "Unable to open config file '%s'\n", cfgfile);
      return 2;
   }

   // Separate host and port
   char *p = strchr(host, ':');
   if (p) {
      *p++ = 0;
      port = atoi(p);
   }

   // Translate host of 0.0.0.0 to localhost
   // This is due to NISIP in apcupsd.conf being 0.0.0.0 for listening on all
   // interfaces. In that case just use loopback.
   if (!strcmp(host, "0.0.0.0"))
      host = DEFAULT_HOST;

   if (!strcmp(cmd, "status"))
   {
      return do_pthreads_status(host, port, par);
   }
   else
   {
      fprintf(stderr, "Unknown command %s\n", cmd);
      usage();
      return 1;
   }
}


------------------------------------------------------------------------------


_______________________________________________
Apcupsd-users mailing list
Apcupsd-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/apcupsd-users


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic