/* $Id: main.c 301 2007-09-20 22:16:34Z fil $ */ /* Copyright 2007 Philip Gwyn. All rights reserved. */ #include #include #include #include #include "apcsnmp.h" static struct option long_options[] = { {"community", 1, 0, 'c'}, {"host", 1, 0, 'h'}, {"port", 1, 0, 'p'}, {"outlet", 1, 0, 'o'}, {"timeout", 1, 0, 't'}, {"verbose", 0, 0, 'v'}, {"off", 0, 0, '0'}, {"on", 0, 0, '1'}, {"cycle", 0, 0, '2'}, {"reset", 0, 0, '2'}, {NULL, 0, NULL, 0} }; host_t host = { NULL, -1, NULL, NULL, NULL, 30 }; static char *outlet_name; static int operation = 1; int verbosity_level = 2; /*******************************************************************/ void usage( void ) { printf( "Usage: apcsnmp -h [host] -c [community] [-v[v]] -o [outlet] " "[--on|--off|--cycle]\n" ); } /*******************************************************************/ int parse_args( int argc, char **argv ) { int c; int option_index; char *bad; while (1) { c = getopt_long( argc, argv, "c:h:t:p:o:v", long_options, &option_index ); if ( c == -1 ) return 0; switch (c) { case 'c': host.community = optarg; LOG( PIL_DEBUG, "Community '%s'", host.community ); break; case 'h': host.name = optarg; LOG( PIL_DEBUG, "Host '%s'", host.name ); break; case 't': host.timeout = strtol( optarg, &bad, 10 ) ; if( errno ) { perror( optarg ); return -1; } LOG( PIL_DEBUG, "Timeout %i", host.timeout ); break; case 'p': host.port = strtol( optarg, &bad, 10 ) ; if( errno ) { perror( optarg ); return -1; } LOG( PIL_DEBUG, "Port %i", host.port ); break; case 'o': outlet_name = optarg; LOG( PIL_DEBUG, "Outlet '%s'", outlet_name ); break; case '2': operation = 2; LOG( PIL_DEBUG, "Cycle power %s the outlet", "on" ); break; case '1': operation = 1; LOG( PIL_DEBUG, "Turn outlet %s", "on" ); break; case '0': operation = 0; LOG( PIL_DEBUG, "Turn outlet %s", "off" ); break; case 'v': verbosity_level +=2; LOG( PIL_CRIT, "Verbosity = %i", verbosity_level ); break; default: usage(); return 1; } } return 0; } /*******************************************************************/ int main(int argc, char ** argv) { int rv = parse_args( argc, argv ); if( rv != 0 ) { return rv; } if( host.name == NULL ) { printf( "Please specify --host\n" ); return 5; } if( host.community == NULL ) { printf( "Please specify --community\n" ); return 6; } if( outlet_name == NULL ) { printf( "Please specify --outlet\n" ); return 7; } if( APC_OK != host_init( &host ) ) return 8; rv = host_identify( &host ); if( rv == APC_OK ) { rv = host_power( &host, outlet_name, operation ); if( rv == APC_OK ) rv = 0; else rv = 10; } else { rv = 11; } host_deinit( &host ); return rv; }