Skip to content

Add a xapi-to-sql command#7

Open
edwintorok wants to merge 1 commit intomseri:masterfrom
edwintorok:sql
Open

Add a xapi-to-sql command#7
edwintorok wants to merge 1 commit intomseri:masterfrom
edwintorok:sql

Conversation

@edwintorok
Copy link
Copy Markdown

This introduces a dependency on sqlite-utils.

When no output file is specified then the SQL is dumped to stdout.

For now nested maps are kept as JSON.
sqlite-utils could flatten,
but it requires all the flattened elements to have the same keys, which is not true for other-config and sm-config, etc.

Here is an example query:

SELECT VM.name_label, host.name_label, VM.domid from VM INNER JOIN VM_guest_metrics ON guest_metrics=VM_guest_metrics.ref INNER JOIN host ON resident_on=host.ref WHERE power_state='Running' AND PV_drivers_detected == "true" AND json_extract(PV_drivers_version, '$.micro')=='-1';

A similar query could be achieved with jq on the output of xapi-to-json, but it is cumbersome:

jq 'JOIN(INDEX(.host[]; .ref); JOIN(INDEX(.VM_guest_metrics[]; .ref); .VM[]; .guest_metrics; .); .[0].resident_on; .) | select(.[0][1].PV_drivers_detected == "true") | select(.[0][1].PV_drivers_version.micro? == "-1" // false) | select(.[0][0].power_state == "Running") | "\(.[0][0].name_label); \(.[1].name_label); \(.[0][0].domid)"'

This introduces a dependency on sqlite-utils.

When no output file is specified then the SQL is dumped to stdout.

For now nested maps are kept as JSON.
sqlite-utils could flatten,
but it requires all the flattened elements to have the same keys,
which is not true for other-config and sm-config, etc.

Here is an example query:
```
SELECT VM.name_label, host.name_label, VM.domid from VM INNER JOIN VM_guest_metrics ON guest_metrics=VM_guest_metrics.ref INNER JOIN host ON resident_on=host.ref WHERE power_state='Running' AND PV_drivers_detected == "true" AND json_extract(PV_drivers_version, '$.micro')=='-1';
```

A similar query could be achieved with `jq` on the output of
`xapi-to-json`, but it is cumbersome:
```
jq 'JOIN(INDEX(.host[]; .ref); JOIN(INDEX(.VM_guest_metrics[]; .ref); .VM[]; .guest_metrics; .); .[0].resident_on; .) | select(.[0][1].PV_drivers_detected == "true") | select(.[0][1].PV_drivers_version.micro? == "-1" // false) | select(.[0][0].power_state == "Running") | "\(.[0][0].name_label); \(.[1].name_label); \(.[0][0].domid)"'
```

Signed-off-by: Edwin Török <edwin.torok@citrix.com>
@edwintorok
Copy link
Copy Markdown
Author

If the added dependency is a concern it might be possible to figure out a way to make it optional, probably needs some fiddling with the python project file. Although keeping it as is would be simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants