[Devel] [Patch] snapshot-list JSON output
Denis Fondras
openvz at ggl.ledeuns.net
Sun Feb 24 02:37:22 PST 2013
Hello,
I made a quick (and dirty) patch to make "vzctl snapshot-list <CTID>"
able to output a JSON structure as it is easier to parse from my Pyhton
management script.
The new option is "-J" and support the "-o" filtering.
I would be very happy if someone could help make it a little less dirty
as I'm more of a sysadmin than a programmer.
Do not hesitate to comment it.
Denis
Here is a sample output :
----8<----------------------------------------
# ./vzctl snapshot-list 7775 -J -o parent_uuid,current,uuid,date,name,desc
{
"snapshot": {
"parent_uuid": "",
"current": "",
"uuid": "{d863723c-a106-4c5e-9037-946249e4826c}",
"date": "2013-02-23 16:59:19",
"name": "",
"desc": ""
},
"snapshot": {
"parent_uuid": "{d863723c-a106-4c5e-9037-946249e4826c}",
"current": "*",
"uuid": "{1e60191e-2a12-4a7f-8d45-216e36148014}",
"date": "2013-02-24 12:27:40",
"name": "Test",
"desc": "Limited to 128 char :-( Hey! I'm not a programmer in the
first place :p"
}
}
----8<----------------------------------------
Here is the patch :
----8<----------------------------------------
diff -ru vzctl-4.2/src/snapshot-list.c vzctl-4.2-json/src/snapshot-list.c
--- vzctl-4.2/src/snapshot-list.c 2013-02-16 02:06:40.000000000 +0100
+++ vzctl-4.2-json/src/snapshot-list.c 2013-02-24 12:10:32.019138573 +0100
@@ -33,81 +33,111 @@
return g_last_field ? "%s\n" : fmt;
}
-static void print_uuid(struct vzctl_snapshot_data *p)
+static void print_uuid(struct vzctl_snapshot_data *p, char *ret)
{
- printf(FMT("%-38s "), p->guid);
+ if (ret == NULL)
+ printf(FMT("%-38s "), p->guid);
+ else {
+ snprintf(ret,128,"%s",p->guid);
+ }
}
-static void print_date(struct vzctl_snapshot_data *p)
+static void print_date(struct vzctl_snapshot_data *p, char *ret)
{
- printf(FMT("%-19s "), p->date ? p->date : "");
+ if (ret == NULL)
+ printf(FMT("%-19s "), p->date ? p->date : "");
+ else {
+ snprintf(ret,128,"%s",p->date ? p->date : "");
+ }
}
-static void print_name(struct vzctl_snapshot_data *p)
+static void print_name(struct vzctl_snapshot_data *p, char *ret)
{
- printf(FMT("%-32s "), p->name ? p->name : "");
+ if (ret == NULL)
+ printf(FMT("%-32s "), p->name ? p->name : "");
+ else {
+ snprintf(ret,128,"%s",p->name ? p->name : "");
+ }
}
-static void print_desc(struct vzctl_snapshot_data *p)
+static void print_desc(struct vzctl_snapshot_data *p, char *ret)
{
- printf(FMT("%-38s "), p->desc ? p->desc : "");
+ if (ret == NULL)
+ printf(FMT("%-38s "), p->desc ? p->desc : "");
+ else {
+ snprintf(ret,128,"%s",p->desc ? p->desc : "");
+ }
}
-static void print_parent_uuid(struct vzctl_snapshot_data *p)
+static void print_parent_uuid(struct vzctl_snapshot_data *p, char *ret)
{
- printf(FMT("%-38s "), p->parent_guid);
+ if (ret == NULL)
+ printf(FMT("%-38s "), p->parent_guid);
+ else {
+ snprintf(ret,128,"%s",p->parent_guid);
+ }
}
-static void print_current(struct vzctl_snapshot_data *p)
+static void print_current(struct vzctl_snapshot_data *p, char *ret)
{
- printf(FMT("%1s "), p->current ? "*" : "");
+ if (ret == NULL)
+ printf(FMT("%1s "), p->current ? "*" : "");
+ else {
+ snprintf(ret,128,"%s",p->current ? "*" : "");
+ }
}
-static void print_device(struct vzctl_snapshot_data *p)
+static void print_device(struct vzctl_snapshot_data *p, char *ret)
{
- char buf[PATH_MAX];
- char dev[PATH_MAX] = "";
+ if (ret == NULL) {
+ char buf[PATH_MAX];
+ char dev[PATH_MAX] = "";
- ploop.set_component_name(g_di,
+ ploop.set_component_name(g_di,
generate_snapshot_component_name(g_envid, p->guid, buf, sizeof(buf)));
- if (ploop.get_dev(g_di, dev, sizeof(dev)))
- goto err;
+ if (ploop.get_dev(g_di, dev, sizeof(dev)))
+ goto err;
- printf(FMT("%-32s "), dev);
- return;
-err:
- printf(FMT("%-32s "), "");
- fprintf(stderr, "%s", ploop.get_last_error());
- error++;
+ printf(FMT("%-32s "), dev);
+ return;
+ err:
+ printf(FMT("%-32s "), "");
+ fprintf(stderr, "%s", ploop.get_last_error());
+ error++;
+ } else {
+ }
}
-static void print_target(struct vzctl_snapshot_data *p)
+static void print_target(struct vzctl_snapshot_data *p, char *ret)
{
- char buf[PATH_MAX];
- char dev[PATH_MAX];
+ if (ret == NULL) {
+ char buf[PATH_MAX];
+ char dev[PATH_MAX];
- ploop.set_component_name(g_di,
+ ploop.set_component_name(g_di,
generate_snapshot_component_name(g_envid, p->guid, buf, sizeof(buf)));
- if (ploop.get_dev(g_di, dev, sizeof(dev)))
- goto err;
- if (ploop.get_mnt_by_dev(dev, buf, sizeof(buf)))
- goto err;
-
- printf(FMT("%-32s "), buf);
- return;
-err:
- printf(FMT("%-32s "), "");
- fprintf(stderr, "%s", ploop.get_last_error());
- error++;
+ if (ploop.get_dev(g_di, dev, sizeof(dev)))
+ goto err;
+ if (ploop.get_mnt_by_dev(dev, buf, sizeof(buf)))
+ goto err;
+
+ printf(FMT("%-32s "), buf);
+ return;
+ err:
+ printf(FMT("%-32s "), "");
+ fprintf(stderr, "%s", ploop.get_last_error());
+ error++;
+ } else {
+ }
}
struct snapshot_field {
char *name;
char *hdr;
char *fmt;
- void (* print_fn)(struct vzctl_snapshot_data *p);
+ void (* print_fn)(struct vzctl_snapshot_data *p, char *);
} field_tbl[] =
{
{"uuid", "UUID", "%-38s", print_uuid},
@@ -240,7 +270,7 @@
static void usage_snapshot_list(int err)
{
fprintf(err ? stderr : stdout,
- "vzctl snapshot-list <ctid|name> [-H] [-o field[,field...]] [--id
<uuid>]\n"
+ "vzctl snapshot-list <ctid|name> [-H] [-J] [-o field[,field...]]
[--id <uuid>]\n"
"vzctl snapshot-list <ctid|name> -L | --list\n"
);
}
@@ -249,7 +279,7 @@
const char *ve_private)
{
int c, ret;
- int no_hdr = 0;
+ int no_hdr = 0, json = 0;
struct vzctl_snapshot_tree *tree = NULL;
struct id_entry *field_entry = NULL;
struct id_entry * uuid_entry = NULL;
@@ -264,13 +294,14 @@
{"uuid", required_argument, NULL, 'u'},
{"id", required_argument, NULL, 'u'},
{"list", no_argument, NULL, 'L'},
+ {"json", no_argument, NULL, 'J'},
{ NULL, 0, NULL, 0 }
};
g_envid = envid;
while (1) {
int option_index = -1;
- c = getopt_long(argc, argv, "HhLo:u:",
+ c = getopt_long(argc, argv, "HJhLo:u:",
list_options, &option_index);
if (c == -1)
break;
@@ -296,6 +327,9 @@
case 'L':
print_names();
return 0;
+ case 'J':
+ no_hdr = json = 1;
+ break;
default:
usage_snapshot_list(1);
return VZ_INVALID_PARAMETER_SYNTAX;
@@ -352,12 +386,34 @@
print_hdr(no_hdr);
- list_for_each(uuid_entry, &g_uuid_list_head, list) {
- list_for_each(field_entry, &g_field_order_head, list) {
- g_last_field = ((void *)g_field_order_head.prev == (void*)field_entry);
- field_tbl[field_entry->id].print_fn(tree->snapshots[uuid_entry->id]);
+ if (json) {
+ int first_json_entry = -1;
+ printf("{");
+ list_for_each(uuid_entry, &g_uuid_list_head, list) {
+ first_json_entry++;
+ if (first_json_entry) printf(",");
+ printf("\n \"snapshot\": {\n");
+ list_for_each(field_entry, &g_field_order_head,
list) {
+ g_last_field = ((void *)g_field_order_head.prev == (void*)field_entry);
+ char ptr[129];
+ memset(ptr,0,129);
+
field_tbl[field_entry->id].print_fn(tree->snapshots[uuid_entry->id], ptr);
+ printf(" \"%s\":
\"%s\"%s\n",field_tbl[field_entry->id].name,ptr,g_last_field?"":",");
+
+ }
+ printf(" }");
}
+ printf("\n}\n");
}
+ else {
+ list_for_each(uuid_entry, &g_uuid_list_head, list) {
+ list_for_each(field_entry, &g_field_order_head, list) {
+ g_last_field = ((void *)g_field_order_head.prev == (void*)field_entry);
+
field_tbl[field_entry->id].print_fn(tree->snapshots[uuid_entry->id], NULL);
+ }
+ }
+ }
+
ploop.free_diskdescriptor(g_di);
return 0;
diff -ru vzctl-4.2/src/vzctl.c vzctl-4.2-json/src/vzctl.c
--- vzctl-4.2/src/vzctl.c 2013-02-16 02:06:40.000000000 +0100
+++ vzctl-4.2-json/src/vzctl.c 2013-02-23 17:28:20.671138739 +0100
@@ -73,7 +73,7 @@
"vzctl snapshot <ctid> [--id <uuid>] [--name <name>] [--description
<desc>]\n"
" [--skip-suspend]\n"
"vzctl snapshot-switch | snapshot-delete <ctid> --id <uuid>\n"
-"vzctl snapshot-list <ctid> [-H] [-o field[,field...]] [--id <uuid>]\n"
+"vzctl snapshot-list <ctid> [-H] [-J] [-o field[,field...]] [--id
<uuid>]\n"
#endif
"vzctl quotaon | quotaoff | quotainit <ctid>\n"
"vzctl console <ctid> [ttyno]\n"
----8<----------------------------------------
More information about the Devel
mailing list