Skip to content

Commit

Permalink
C: Add "job_query" example which queries job counters
Browse files Browse the repository at this point in the history
It's primarily intended for looking at the counters of MTP-2
signalling jobs.
  • Loading branch information
Matthias Lang committed Aug 10, 2016
1 parent 447e9ad commit 0c77c91
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 2 deletions.
1 change: 1 addition & 0 deletions c/.gitignore
Expand Up @@ -4,6 +4,7 @@ connect_timeslots
duplex_lapd
disable
enable
job_query
query_set
install_release
install_start_script
Expand Down
2 changes: 1 addition & 1 deletion c/Makefile
Expand Up @@ -10,6 +10,7 @@ target_basenames=\
enable \
install_release \
install_start_script \
job_query \
map \
monitor_dtmf \
playback_file\
Expand Down Expand Up @@ -63,4 +64,3 @@ version.c:

%.exe: %.c $(common_c_files) version.c
$(CC_WIN32) -Wall -Wextra -o $@ $^ $(LIBS_WIN32)

3 changes: 2 additions & 1 deletion c/NMakefile
Expand Up @@ -7,6 +7,7 @@ targets=\
enable.exe \
install_release.exe\
install_start_script.exe\
job_query.exe\
map.exe\
monitor_dtmf.exe\
playback_file.exe\
Expand Down Expand Up @@ -39,4 +40,4 @@ $(targets): $(header_files) $(common_c_files) version.c
version.c:
echo #include "gth_apilib.h" > version.c
echo const char git_head[] = "VS build at %DATE% %TIME%"; >> version.c
echo const char build_hostname[] = "%COMPUTERNAME%"; >> version.c
echo const char build_hostname[] = "%COMPUTERNAME%"; >> version.c
60 changes: 60 additions & 0 deletions c/gth_apilib.c
Expand Up @@ -1531,6 +1531,66 @@ int gth_query_resource_attribute(GTH_api *api,
return retval;
}

int gth_query_job(GTH_api *api,
const char *id,
char *owner,
GTH_attribute **attributes,
int *n_attributes)
{
char buffer[MAX_COMMAND];
GTH_resp *resp;
GTH_resp *job;
char *owner_copy;
int x;
int retval = 0;

assert(api);
assert(id);
assert(attributes);
assert(n_attributes);

snprintf(buffer, MAX_COMMAND, "<query><job id='%s'/></query>", id);
api_write(api, buffer);
resp = gth_next_non_event(api);

if (resp == 0) return -9;

if (resp->type == GTH_RESP_STATE
&& resp->n_children == 1)
{
job = resp->children;

owner_copy = attribute_value(job, "owner");
strncpy_s(owner, MAX_JOB_ID, owner_copy, MAX_JOB_ID - 1);

*n_attributes = job->n_children;
*attributes = checked_malloc(sizeof(GTH_attribute) * *n_attributes);

for (x = 0; x < job->n_children; x++) {
GTH_resp *attribute;
char *name;
char *value;

attribute = job->children+x;
if (attribute->type != GTH_RESP_ATTRIBUTE)
die("invalid response from GTH");

name = attribute_value_and_clear(attribute, "name");
value = attribute_value_and_clear(attribute, "value");

(*attributes)[x].key = name;
(*attributes)[x].value = value;
}
}
else
{
retval = -1;
}

gth_free_resp(resp);
return retval;
}

static int is_text_following_resource_query(const char *name)
{
assert(name);
Expand Down
16 changes: 16 additions & 0 deletions c/gth_apilib.h
Expand Up @@ -424,6 +424,22 @@ int gth_set_single(GTH_api *api,
// Reset. The only valid resource is "cpu".
int gth_reset(GTH_api *api, const char *resource);

// Query a job
//
// 'owner' must point to an array of at least MAX_JOB bytes
//
// This function creates an array of **attributes on the heap; the caller
// is responsible for calling gth_free_attributes() to free them.
//
// *n_attributes is set to the number of attributes returned
//
// Return: 0 on success.
int gth_query_job(GTH_api *api,
const char *id,
char *owner,
GTH_attribute **attributes,
int *n_attributes);

// Query one attribute on a resource. Returns the value in result, using up
// to max_size characters, including the terminating 0.
//
Expand Down
2 changes: 2 additions & 0 deletions c/gth_client_xml_parse.c
Expand Up @@ -504,9 +504,11 @@ static enum Token_type name_to_token_type(const char* name)
if (!strcmp(name, "l2_alarm")) return GTH_RESP_L2_ALARM;
if (!strcmp(name, "l2_socket_alert")) return GTH_RESP_L2_SOCKET_ALERT;
if (!strcmp(name, "lapd_message")) return GTH_RESP_LAPD_MESSAGE;
if (!strcmp(name, "lapd_monitor")) return GTH_RESP_LAPD_MONITOR;
if (!strcmp(name, "level")) return GTH_RESP_LEVEL;
if (!strcmp(name, "message_ended")) return GTH_RESP_MESSAGE_ENDED;
if (!strcmp(name, "mtp2_message")) return GTH_RESP_MTP2_MESSAGE;
if (!strcmp(name, "mtp2_monitor")) return GTH_RESP_MTP2_MONITOR;
if (!strcmp(name, "ok")) return GTH_RESP_OK;
if (!strcmp(name, "resource")) return GTH_RESP_RESOURCE;
if (!strcmp(name, "sdh_message")) return GTH_RESP_SDH_MESSAGE;
Expand Down
2 changes: 2 additions & 0 deletions c/gth_client_xml_parse.h
Expand Up @@ -73,10 +73,12 @@ typedef enum {
GTH_RESP_L2_ALARM,
GTH_RESP_L2_SOCKET_ALERT,
GTH_RESP_LAPD_MESSAGE,
GTH_RESP_LAPD_MONITOR,
GTH_RESP_LEVEL,
GTH_RESP_MESSAGE_ENDED,

GTH_RESP_MTP2_MESSAGE,
GTH_RESP_MTP2_MONITOR,
GTH_RESP_OK,
GTH_RESP_RESOURCE,
GTH_RESP_SDH_MESSAGE,
Expand Down
125 changes: 125 additions & 0 deletions c/job_query.c
@@ -0,0 +1,125 @@
//----------------------------------------------------------------------
// Query job attributes on a GTH
//
// Doesn't attempt any error handling.
//
// Author: Matt Lang (matthias@corelatus.se)
//
// Copyright (c) 2016, Corelatus AB Stockholm
//
// All rights reserved. Licence: BSD.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Corelatus nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY Corelatus ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL Corelatus BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>

#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#endif // WIN32

#include "gth_win32_compat.h"
#include "gth_apilib.h"

static void usage()
{
fprintf(stderr,
"job_query git_head: %s build_hostname: %s\n\n"

"job_query [-v] <GTH-IP> <id>\n\n"
"Query job attributes on a GTH.\n\n"

"-v: print the API commands and responses (verbose)\n"
"<GTH-IP> is the GTH's IP address or hostname\n"
"<id> is the ID of a job (hint: the 'schedule' resource is a list of jobs)\n"

"Examples:\n"
"./job_query 172.16.1.10 m2mo3\n",
git_head, build_hostname);

exit(-1);
}

static void query_job(GTH_api *api, const char *id)
{
int n_attributes;
GTH_attribute *attrs;
char owner[MAX_JOB_ID + 1];
int result;
int x;

result = gth_query_job(api, id, owner, &attrs, &n_attributes);
if (result != 0)
{
fprintf(stderr, "unable to query %s\n", id);
gth_bye(api);
exit(-1);
}

for (x = 0; x < n_attributes; x++)
printf("%s=%s\n", attrs[x].key, attrs[x].value);
}


#define MAX_ATTRIBUTES 100

// Entry point
int main(int argc, char** argv)
{
int result;
GTH_api api;
int verbose = 0;

while (argc > 1 && argv[1][0] == '-') {
switch (argv[1][1]) {
case 'v': verbose = 1; break;

default: usage();
}
argc--;
argv++;
}

if (argc != 3) {
usage();
}

win32_specific_startup();

result = gth_connect(&api, argv[1], verbose);
if (result != 0) {
die("Unable to connect to the GTH. Giving up.");
}

query_job(&api, argv[2]);

gth_bye(&api);

return 0;
}

0 comments on commit 0c77c91

Please sign in to comment.