Child pages
  • Creating a Query
Skip to end of metadata
Go to start of metadata


Queries are an important part of Open-AudIT. They are what enables you to get meaningful information from all the device data you have discovered. Along with summaries and groups, they provide a powerful mechanism to extract crucial information.

How Does It Work?

A query is essentially a SQL statement. This statement is run against the database with the automatic addition of the limit, filtered to apply to only those requested items and only those items the user has permission to view. A Query can be created using menu -> Manage -> Queries -> Create Queries. Queries contain an org_id and are hence restricted to the appropriate users. A user must have the org_admin or reporter role(s) to create, update or delete a query. All users can execute a query. A query has a name attribute used for the menu item as well as a menu category attribute. This tells the Open-Audit GUI which submenu to place the query in. There is also menu display which should be set to 'y' to enable the query in the GUI ('n' to prevent the query from appearing at all). The query would still run if called using it's $id, regardless of menu display's value.

To view the details of a query, the standard URL structure of /open-audit/index.php/queries/{$id} should be used.

To actually execute the query, append a /execute, thus /open-audit/index.php/queries/{$id}/execute. Alternatively, a URL format of /open-audit/index.php/devices?sub_resource=query&sub_resource_id={$id} can be used. That is how the Open-AudIT GUI does it.

 Breaking it Down

The SQL query is essentially broken into three parts.


The SELECT section of the query should use full dot notation and also request the field with it's full dot name. IE - SELECT AS ``. Each field should be selected in this fashion to enable GUI side attribute filtering.


You should use only those tables that contain attributes you need. I usually use a LEFT JOIN. IE - SELECT AS ``, ip.ip AS `ip.ip` FROM system LEFT JOIN ip ON ( = ip.system_id).

All device subtables contain a couple of important columns. $table.system_id and $table.current. $table.system_id is the link to the devices column. The $table.current column will contain either 'y' or 'n'. This indicates if this row is currently present on the device. For example software may be installed (which would result in software.current = 'y'), but on a subsequent audit it may not be detected. Open-AudIT will then change this rows current attribute to 'n'.


In order for Open-AudIT to be able to apply user permissions on items, we mandate the user of WHERE @filter. If you do not use this format, the query::create form will throw a warning. Only users with the Admin role are permitted to create queries that lack this attribute.

Other than that restriction, you are free to select attributes as required. It's a good idea to use the menu -> Admin -> Database -> List Tables item to view the specific tables and their columns. This will enable you to find exactly what you need, rather than trawling through the MySQL console of the schema creation script.

Refining the Query

Using the web GUI, you can select items to be included or excluded. You can also select a group of devices, then run the query upon that group.



NOTE - The SQL queries used in Open-AudIT require the use of the backtick - ` character and NOT the standard single quote for fields. On most US Windows keyboards the backtick key is located in the top-left of the keyboard along with the tilde ~. On a US Mac keyboard the backtick key is located next to the SHIFT key. The standard single quote is still used to enclose values as the examples below illustrate.

The Elevated User query

Devices Older Than X

This example query retrieves a list of devices OVER 3 years old. The query uses today (NOW) and system.purchase_date as the reference point and filters out all virtual machines via a check of the system.serial field for %VM%.

Devices with Expired Warranties

This example uses system.warranty_expires and looks for a warranty expiration date prior to today.



Devices Missing Information

This example creates a list of devices where the Function or Description fields are blank OR the Purchase Date is the default.

List All NMAP Ports, Protocols and Programs for Each Device

This example creates a list of devices and the open Ports, Protocols, and Programs found by the NMAP scan.

Return a list of all Modules installed on Routers

This example creates a list of all Modules marked as current='y' on devices of type 'router'