File: nws_insert.c

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (135 lines) | stat: -rw-r--r-- 3,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* $Id: nws_insert.c,v 1.32 2003/05/23 17:12:20 graziano Exp $ */

#include "config_nws.h"
#include <stdio.h>       /* file functions */
#include <stdlib.h>      /* atoi() */
#include <string.h>      /* strncasecmp() */
#include <strings.h>     /* strncasecmp() on aix */
#include <unistd.h>      /* getopt() */
#include "diagnostic.h"  /* ABORT() */
#define NWSAPI_SHORTNAMES
#include "nws_api.h"     /* NWS programming interface */


/*
** This program allows users to store measurements in NWS memories from the
** command line.  See the USERSGUIDE for a description of the command-line
** options.
*/


#define SAFE_STRCPY(to, from) \
  do {strncpy(to, from, sizeof(to)); to[sizeof(to) - 1] = '\0'; if (1) break;} while (0)


#define LINE_LENGTH 80
#define MAX_MEASUREMENTS 2000
#define SWITCHES "f:M:"
#define DEFAULT_MEMORY_PORT "8050"

int
main(int argc,
     char **argv) {

  extern char *optarg;
  extern int optind;
  FILE *inputFile = stdin;
  char inputLine[LINE_LENGTH];
  Measurement measurements[MAX_MEASUREMENTS];
  size_t lineCount;
  size_t measurementCount;
  HostSpec memorySpec;
  int opt;
  int optlen;
  SeriesSpec series;
  const char *USAGE = "nws_insert [-f file] [-M host] resource host [host]";

  DirectDiagnostics(DIAGINFO, stdout);
  DirectDiagnostics(DIAGLOG, stdout);
  DirectDiagnostics(DIAGWARN, stderr);
  DirectDiagnostics(DIAGERROR, stderr);
  DirectDiagnostics(DIAGFATAL, stderr);

  while((opt = getopt(argc, argv, SWITCHES)) != EOF) {

    switch(opt) {

    case 'f':
      if(inputFile != stdin)
        fclose(inputFile);
      inputFile = fopen(optarg, "r");
      if(inputFile == NULL)
        ABORT1("Unable to open file %s\n", optarg);
      break;

    case 'M':
      memorySpec = *MakeHostSpec
        (optarg, atoi(EnvironmentValue("MEMORY_PORT", DEFAULT_MEMORY_PORT)));
      if(!UseMemory(&memorySpec))
        ABORT2("Unable to contact memory %s:%d\n",
               memorySpec.machineName, memorySpec.machinePort);
      break;

    default:
      fprintf(stderr, "nws_insert: unrecognized switch\n%s\n", USAGE);
      exit(1);
      break;

    }

  }

  if((optind + 1) >= argc) {
    fprintf(stderr, "%s\n", USAGE);
    exit(1);
  }

  optarg = argv[optind];
  optlen = strlen(optarg);
  SAFE_STRCPY(series.resourceName,
    (strncasecmp(optarg, DEFAULT_BANDWIDTH_RESOURCE, optlen) == 0) ?
       DEFAULT_BANDWIDTH_RESOURCE :
    (strncasecmp(optarg, DEFAULT_AVAILABLE_CPU_RESOURCE, optlen) == 0)  ?
       DEFAULT_AVAILABLE_CPU_RESOURCE :
    (strncasecmp(optarg, DEFAULT_CURRENT_CPU_RESOURCE, optlen) == 0)   ?
       DEFAULT_CURRENT_CPU_RESOURCE :
    (strncasecmp(optarg, DEFAULT_LATENCY_RESOURCE, optlen) == 0)   ?
       DEFAULT_LATENCY_RESOURCE :
    (strncasecmp(optarg, DEFAULT_MEMORY_RESOURCE, optlen) == 0)   ?
       DEFAULT_MEMORY_RESOURCE :
    optarg);
  SAFE_STRCPY(series.sourceMachine, argv[optind + 1]);

  if((optind + 2) < argc)
    SAFE_STRCPY(series.destinationMachine, argv[optind + 2]);
  else if(IntermachineResource(series.resourceName))
    ABORT("You must specify a destination machine\n");
  else
    SAFE_STRCPY(series.destinationMachine, "");

  lineCount = 0;
  measurementCount = 0;
  while(fgets(inputLine, sizeof(inputLine), inputFile) != NULL) {
    if( (strcmp(inputLine, "\n") != 0) &&
        (inputLine[0] != '#') ) {
      if(sscanf(inputLine, "%lf%lf",
                &measurements[measurementCount].timeStamp,
                &measurements[measurementCount].measurement) != 2)
        ABORT2("Bad format in line %d: %s\n", lineCount, inputLine);
      measurementCount++;
    }
    lineCount++;
  }

  if(inputFile != stdin)
    (void)fclose(inputFile);

  if(!PutMeasurements(SeriesName(&series),
                      &series,
                      measurements,
                      measurementCount))
    ABORT("Attempt to store experiments failed\n");

  exit(0);

}