| 1 | /********************************************************************** |
|---|
| 2 | * Copyright (c) 2005-2008 David Carter <dcarter@arm4.org> and others. |
|---|
| 3 | * All rights reserved. This program and the accompanying materials |
|---|
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
|---|
| 5 | * which accompanies this distribution, and is available at |
|---|
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
|---|
| 7 | * |
|---|
| 8 | * Contributors: |
|---|
| 9 | * David Carter - Initial API and implementation |
|---|
| 10 | **********************************************************************/ |
|---|
| 11 | |
|---|
| 12 | #include "config.h" |
|---|
| 13 | |
|---|
| 14 | #include <sys/types.h> |
|---|
| 15 | #include <sys/time.h> |
|---|
| 16 | #include <errno.h> |
|---|
| 17 | |
|---|
| 18 | #include <stddef.h> |
|---|
| 19 | #include <stdio.h> |
|---|
| 20 | #include <stdlib.h> |
|---|
| 21 | #include <string.h> |
|---|
| 22 | #include <math.h> |
|---|
| 23 | #include <arpa/inet.h> |
|---|
| 24 | |
|---|
| 25 | #ifdef HAVE_GETOPT_H |
|---|
| 26 | #include <getopt.h> |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | #include <db.h> |
|---|
| 30 | #include "arm4.h" |
|---|
| 31 | #include "Arm4db.h" |
|---|
| 32 | #include "Logger.h" |
|---|
| 33 | #include "Arm4dbSharedMemory.h" |
|---|
| 34 | #include "arm4agent.h" |
|---|
| 35 | |
|---|
| 36 | static int g_detailed = 0; |
|---|
| 37 | static int g_summary = 0; |
|---|
| 38 | |
|---|
| 39 | #if 0 |
|---|
| 40 | // Not currently used |
|---|
| 41 | static int |
|---|
| 42 | time_less (const arm_time_t *time1_ptr, const arm_time_t *time2_ptr) |
|---|
| 43 | { |
|---|
| 44 | if (time1_ptr->tv_sec < time2_ptr->tv_sec) |
|---|
| 45 | return ARM_TRUE; |
|---|
| 46 | else if ((time1_ptr->tv_sec == time2_ptr->tv_sec) && |
|---|
| 47 | (time1_ptr->tv_usec < time2_ptr->tv_usec)) |
|---|
| 48 | return ARM_TRUE; |
|---|
| 49 | |
|---|
| 50 | return ARM_FALSE; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | static int |
|---|
| 54 | time_greater (const arm_time_t *time1_ptr, const arm_time_t *time2_ptr) |
|---|
| 55 | { |
|---|
| 56 | if (time1_ptr->tv_sec > time2_ptr->tv_sec) |
|---|
| 57 | return ARM_TRUE; |
|---|
| 58 | else if ((time1_ptr->tv_sec == time2_ptr->tv_sec) && |
|---|
| 59 | (time1_ptr->tv_usec > time2_ptr->tv_usec)) |
|---|
| 60 | return ARM_TRUE; |
|---|
| 61 | |
|---|
| 62 | return ARM_FALSE; |
|---|
| 63 | } |
|---|
| 64 | #endif |
|---|
| 65 | |
|---|
| 66 | static void |
|---|
| 67 | initialize_configuration (const char *archive_directory, const char *config_filename) |
|---|
| 68 | { |
|---|
| 69 | // Set the database configuration |
|---|
| 70 | Arm4dbConfig::setConfig (config_filename); |
|---|
| 71 | |
|---|
| 72 | if (strlen (archive_directory) > 0) |
|---|
| 73 | Arm4dbConfig::setDBHome (archive_directory); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | static void |
|---|
| 77 | version (void) |
|---|
| 78 | { |
|---|
| 79 | printf ("Package %s\n", PACKAGE); |
|---|
| 80 | printf ("Version %s\n", VERSION); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | static void |
|---|
| 84 | usage (void) |
|---|
| 85 | { |
|---|
| 86 | printf ("Usage:\n"); |
|---|
| 87 | #ifdef HAVE_GETOPT_LONG |
|---|
| 88 | printf ("\tarm4_report --version|-V\n"); |
|---|
| 89 | printf ("\t\tDisplay implementation version\n"); |
|---|
| 90 | printf ("\tarm4_report [--summary|-s] [--detailed|-d] [--help|-h] [--config|-C config_file] [--archive|-a archive_directory]\n"); |
|---|
| 91 | #else |
|---|
| 92 | printf ("\tarm4_report [-s] [-d] [-h] [-V] [-C config_file] [-a archive_directory]\n"); |
|---|
| 93 | #endif |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | int |
|---|
| 97 | main(int argc, char *argv[]) |
|---|
| 98 | { |
|---|
| 99 | int rc = 0; |
|---|
| 100 | int c; |
|---|
| 101 | char config_filename [1024]; |
|---|
| 102 | char archive_directory [1024]; |
|---|
| 103 | |
|---|
| 104 | // Initialize the logger |
|---|
| 105 | Logger::getLogger().setName ("arm4_report"); |
|---|
| 106 | Logger::getLogger().setFile (stderr); |
|---|
| 107 | |
|---|
| 108 | /* Set the default config file */ |
|---|
| 109 | strcpy (config_filename, "/etc/arm4.conf"); |
|---|
| 110 | archive_directory [0] = '\0'; |
|---|
| 111 | |
|---|
| 112 | while (1) |
|---|
| 113 | { |
|---|
| 114 | #ifdef HAVE_GETOPT_LONG |
|---|
| 115 | static struct option long_options[] = |
|---|
| 116 | { |
|---|
| 117 | /* These options set a flag. */ |
|---|
| 118 | {"summary", no_argument, &g_summary, 1}, |
|---|
| 119 | {"detailed", no_argument, &g_detailed, 1}, |
|---|
| 120 | /* These options don't set a flag. We distinguish them by their indices. */ |
|---|
| 121 | {"help", no_argument, 0, 'h'}, |
|---|
| 122 | {"version", no_argument, 0, 'V'}, |
|---|
| 123 | {"config", required_argument, 0, 'C'}, |
|---|
| 124 | {"archive", required_argument, 0, 'a'}, |
|---|
| 125 | |
|---|
| 126 | {0, 0, 0, 0} |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | /* getopt_long stores the option index here. */ |
|---|
| 130 | int option_index = 0; |
|---|
| 131 | |
|---|
| 132 | c = getopt_long (argc, argv, "a:dhsVC:", |
|---|
| 133 | long_options, &option_index); |
|---|
| 134 | #else |
|---|
| 135 | c = getopt (argc, argv, "a:dhsVC:"); |
|---|
| 136 | #endif |
|---|
| 137 | /* Detect the end of the options. */ |
|---|
| 138 | if (c == -1) |
|---|
| 139 | break; |
|---|
| 140 | |
|---|
| 141 | switch (c) |
|---|
| 142 | { |
|---|
| 143 | case 0: |
|---|
| 144 | /* If this option set a flag, do nothing else now. */ |
|---|
| 145 | #ifdef HAVE_GETOPT_LONG |
|---|
| 146 | if (long_options[option_index].flag != 0) |
|---|
| 147 | break; |
|---|
| 148 | |
|---|
| 149 | printf ("option %s", long_options[option_index].name); |
|---|
| 150 | if (optarg) |
|---|
| 151 | printf (" with arg %s", optarg); |
|---|
| 152 | printf ("\n"); |
|---|
| 153 | #endif |
|---|
| 154 | break; |
|---|
| 155 | |
|---|
| 156 | case 'a': |
|---|
| 157 | strncpy (archive_directory, optarg, sizeof(archive_directory)); |
|---|
| 158 | break; |
|---|
| 159 | |
|---|
| 160 | case 'd': |
|---|
| 161 | g_detailed = 1; |
|---|
| 162 | break; |
|---|
| 163 | |
|---|
| 164 | case 's': |
|---|
| 165 | g_summary = 1; |
|---|
| 166 | break; |
|---|
| 167 | |
|---|
| 168 | case '?': |
|---|
| 169 | /* getopt_long already printed an error message. */ |
|---|
| 170 | break; |
|---|
| 171 | |
|---|
| 172 | case 'h': |
|---|
| 173 | usage (); |
|---|
| 174 | exit (0); |
|---|
| 175 | |
|---|
| 176 | case 'V': |
|---|
| 177 | version (); |
|---|
| 178 | exit (0); |
|---|
| 179 | |
|---|
| 180 | case 'C': |
|---|
| 181 | strncpy (config_filename, optarg, sizeof(config_filename)); |
|---|
| 182 | break; |
|---|
| 183 | |
|---|
| 184 | default: |
|---|
| 185 | usage (); |
|---|
| 186 | abort (); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | initialize_configuration (archive_directory, config_filename); |
|---|
| 191 | |
|---|
| 192 | Arm4dbUtilityLock lock; |
|---|
| 193 | |
|---|
| 194 | Arm4db *arm4db_ptr = Arm4db::getArm4db (); |
|---|
| 195 | int status = arm4db_ptr->openDatabases (); |
|---|
| 196 | if (status != ARM_SUCCESS) |
|---|
| 197 | { |
|---|
| 198 | lock.unlock (); |
|---|
| 199 | return 1; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /* At some point we should include options to specify what our report contains */ |
|---|
| 203 | if (g_summary || (g_detailed == 0)) |
|---|
| 204 | arm4db_ptr->printSummaries (); |
|---|
| 205 | if (g_detailed) |
|---|
| 206 | arm4db_ptr->printTransactions(); |
|---|
| 207 | |
|---|
| 208 | arm4db_ptr->closeDatabases (); |
|---|
| 209 | |
|---|
| 210 | lock.unlock (); |
|---|
| 211 | |
|---|
| 212 | return rc; |
|---|
| 213 | } |
|---|