CLI#
Mainflux CLI makes it easy to manage users, things, profiles, groups, orgs, webhooks and messages.
CLI can be downloaded as a pre-built binary from the project releases or it can be built with GNU Make tool:
git clone https://github.com/MainfluxLabs/mainflux
cd mainflux
make cli
which will build mainfluxlabs-cli in <project_root>/build folder.
Executing build/mainfluxlabs-cli without any arguments will output help with all available commands and flags:
Usage:
mainfluxlabs-cli [command]
Available Commands:
benchmark Provision entities and run an HTTP JSON message load benchmark
certs Certificates management
completion Generate the autocompletion script for the specified shell
group_memberships Group memberships management
groups Groups management
health Health Check
help Help about any command
keys Keys management
messages Send, read, delete, export, backup, or restore messages
org_memberships Org memberships management
orgs Orgs management
profiles Profiles management
provision Provision things and profiles from a config file
things Things management
users Users management
webhooks Webhooks management
Flags:
--agg-field string Aggregation fields (comma-separated)
--agg-interval string Aggregation interval (minute, hour, day, week, month, year)
--agg-type string Aggregation type (min, max, avg, count)
--agg-value uint Aggregation value (default 1)
-a, --auth-url string Auth service URL (default "http://localhost/svcauth")
--bool-value SenML boolean value filter
-c, --certs-url string Certs service URL (default "http://localhost")
--comparator string Comparison operator (SenML: eq, lt, le, gt, ge; JSON: eq, starts_with, contains)
-y, --content-type string Message content type (default "application/senml+json")
--convert string Export format (json/csv) (default "json")
--data-value string SenML data value filter
--dir string Sort direction (asc/desc)
-e, --email string User email query parameter
-f, --format string Message format query parameter
--from int From timestamp in milliseconds
-h, --help help for mainfluxlabs-cli
-p, --http-url string HTTP adapter URL (default "http://localhost/http")
-i, --insecure Do not check for TLS cert
--key string JSON payload key
-l, --limit uint Limit query parameter (default 100)
-m, --metadata string Metadata query parameter
-n, --name string Name query parameter
-o, --offset uint Offset query parameter
--protocol string Protocol query parameter
-r, --raw Enables raw output mode for easier parsing of output
--senml-name string SenML name filter
--senml-value float SenML numeric value filter
--string-value string SenML string value filter
-s, --subtopic string Subtopic query parameter
--thing-id string Thing ID query parameter
-t, --things-url string Things service URL (default "http://localhost/svcthings")
--time-format string Export time format
--to int To timestamp in milliseconds
--url string Deployment base URL from which service URLs that are not set explicitly are derived
-u, --users-url string Users service URL (default "http://localhost/svcusers")
--value string JSON payload value
-w, --webhooks-url string Webhooks service URL (default "http://localhost/svcwebhooks")
Use "mainfluxlabs-cli [command] --help" for more information about a command.
It is also possible to use the docker image mainfluxlabs/cli to execute CLI command:
docker run -it --rm mainfluxlabs/cli --url http://<IP_SERVER> [command]
You can execute each command with -h flag for more information about that command, e.g.
mainfluxlabs-cli profiles -h
will get you usage info:
Profiles management: create, get, update or delete Profiles and get Profile by Thing
Usage:
mainfluxlabs-cli profiles [flags]
mainfluxlabs-cli profiles [command]
Available Commands:
create create <JSON_profile> <group_id> <user_token>
delete delete <profile_id> <user_token>
get get [all | thing | by-id] <user_token> <id>
update update <JSON_string> <profile_id> <user_token>
Service#
Check Things Service Health#
mainfluxlabs-cli health
Users management#
Create User#
Within Mainflux, the admin creates users.
The <user_token> is required because the token is used to verify that the requester is admin or not.
For more details, please see Authorization page.
mainfluxlabs-cli users create <user_email> <user_password> <user_token>
Login User#
mainfluxlabs-cli users token <user_email> <user_password>
Retrieve User#
mainfluxlabs-cli users get <all | user_id> <user_token>
Only the admin can retrieve all users or another user's user_id; other users can retrieve only their own user_id.
Update User Metadata#
mainfluxlabs-cli users update '{"key1":"value1", "key2":"value2"}' <user_token>
Update User Password#
mainfluxlabs-cli users password <old_password> <password> <user_token>
System Provisioning#
Provision Things#
Provision multiple Things in bulk from a JSON / CSV input file.
mainfluxlabs-cli provision things <file> <profile_id> <user_token>
file- A path to a CSV or JSON file containing Things (must have the.jsonor.csvextension)profile_id- ID of the Profile to which the Things are assigneduser_token- A valid user auth token for the current system
Input file structure (JSON):#
The root of the JSON input file must be an array containing objects, each one representing a separate Thing, each of which can have the following fields:
nameidtypekeymetadata
See API -> Things for the meaning and explanation of these fields.
Example:
[
{
"name": "thing0",
"type": "device",
"metadata": {
"locationCountry": "Poland",
"locationCity": "Warsaw",
"a": "1"
}
},
{
"name": "thing1",
"type": "sensor",
"metadata": {
"b": 2
}
},
...
]
Input file structure (CSV):#
The mandatory columns of each CSV record are the following, in order:
- ID
- Name
- Type
Any remaining columns are interpreted as metadata key-value pairs. The values are interpreted and stored as JSON strings. See example below.
Example:
,thing0,device,locationCountry,Poland,locationCity,Warsaw,a,1
The above CSV record and the first object in the JSON example represent the same Thing.
Provision Profiles#
Provision multiple Profiles in bulk from a JSON / CSV input file.
mainfluxlabs-cli provision profiles <file> <group_id> <user_token>
file- A path to a CSV or JSON file containing profiles (must have the.jsonor.csvextension)group_id- ID of the Group in which the Profiles are createduser_token- A valid user auth token for the current system
Input file structure (JSON):#
The root of the JSON input file must be an array containing objects, each one representing a separate Profile, each of which can have the following fields:
nameidconfigmetadata
See API -> Profiles for the meaning and explanation of these fields.
Example:
[
{
"id": "473c97ca-ac23-4e0f-b679-c34bcde98491",
"name": "profile0",
"config": {
"content_type": "application/json",
"write_enabled": false,
"transformer": {
"data_filters": ["val1", "val2"],
"data_field": "data.data.data",
"time_field": "t",
"time_format": "unix",
"time_location": "UTC"
}
},
"metadata": {
"a": "1"
}
},
{
"id": "33da57ee-f072-4eef-a860-c3d2292e8c07",
"name": "profile1",
"config": {...},
"metadata": {
"b": 2
}
},
...
]
Input file structure (CSV):#
The mandatory columns of each CSV record are the following, in order:
- ID
- Name
- Config - "content_type"
- Config - "write_enabled"
- Config - "transformer" - "data_filters"
- Config - "transformer" - "data_field"
- Config - "transformer" - "time_field"
- Config - "transformer" - "time_format"
- Config - "transformer" - "time_location"
Any remaining columns are interpreted as metadata key-value pairs. The values are interpreted and stored as JSON strings. See example below.
Example:
473c97ca-ac23-4e0f-b679-c34bcde98491,profile0,application/json,false,"val1,val2","data.data.data",t,unix,UTC,a,1
The above CSV record and the first object in the JSON example represent the same Profile.
Things#
Create Thing#
The type field is required (device, sensor, actuator, controller, gateway). The profile_id is a separate CLI argument, not part of the JSON.
mainfluxlabs-cli things create '{"name":"<thing_name>","type":"device"}' <profile_id> <user_token>
Create Thing with metadata#
mainfluxlabs-cli things create '{"name":"<thing_name>","type":"device","metadata":{"key1":"value1"}}' <profile_id> <user_token>
Update Thing#
mainfluxlabs-cli things update '{"name":"<new_name>","profile_id":"<profile_id>"}' <thing_id> <user_token>
Remove Thing#
mainfluxlabs-cli things delete <thing_id> <user_token>
Retrieve a subset list of provisioned Things#
mainfluxlabs-cli things get all <user_token>
Retrieve Thing by ID#
mainfluxlabs-cli things get by-id <thing_id> <user_token>
Retrieve Things by Profile#
mainfluxlabs-cli things get by-profile <profile_id> <user_token>
Retrieve Metadata by Key#
mainfluxlabs-cli things metadata <key_type> <thing_key>
Retrieve Thing ID by Key#
mainfluxlabs-cli things identify <key_type> <thing_key>
Profiles#
Create Profile#
mainfluxlabs-cli profiles create '{"name":"<profile_name>"}' <group_id> <user_token>
Update Profile#
mainfluxlabs-cli profiles update '{"name":"<new_name>"}' <profile_id> <user_token>
Remove Profile#
mainfluxlabs-cli profiles delete <profile_id> <user_token>
Retrieve a subset list of provisioned Profiles#
mainfluxlabs-cli profiles get all <user_token>
Retrieve Profile by ID#
mainfluxlabs-cli profiles get by-id <profile_id> <user_token>
Retrieve a Profile by Thing#
mainfluxlabs-cli profiles get by-thing <thing_id> <user_token>
Messaging#
Send a message over HTTP#
The key_type argument is either internal (platform-generated key) or external (custom key). An optional subtopic can be prepended as the first argument.
# Without subtopic
mainfluxlabs-cli messages send '[{"bn":"Dev1","n":"temp","v":20},{"n":"hum","v":40}]' internal <thing_key>
# With subtopic
mainfluxlabs-cli messages send temperature '[{"n":"temp","v":25.3}]' internal <thing_key>
Read messages over HTTP#
# Read SenML messages (as thing)
mainfluxlabs-cli messages read senml internal <thing_key> <user_token>
# Read JSON messages (as thing)
mainfluxlabs-cli messages read json internal <thing_key> <user_token>
# Read as admin (no thing key required)
mainfluxlabs-cli messages read senml <user_token>
mainfluxlabs-cli messages read json <user_token>
Use -s=<subtopic> to filter by subtopic.
Back up and restore messages#
Both commands require admin privileges. backup writes the backup to standard output.
mainfluxlabs-cli messages backup <user_token> > backup.json
mainfluxlabs-cli messages restore backup.json <user_token>
Groups#
Create Group#
mainfluxlabs-cli groups create '{"name":"<group_name>","description":"<description>","metadata":{"key":"value",...}}' <org_id> <user_token>
Delete Group#
mainfluxlabs-cli groups delete <group_id> <user_token>
Get Group by ID#
mainfluxlabs-cli groups get <group_id> <user_token>
List all Groups#
mainfluxlabs-cli groups get all <user_token>
List Groups by Org#
mainfluxlabs-cli groups get by-org <org-id> <user_token>
Update Group#
mainfluxlabs-cli groups update '{"name":"<new_name>"}' <group_id> <user_token>
List Things by Group#
mainfluxlabs-cli groups things <group_id> <user_token>
View Group by Thing#
mainfluxlabs-cli groups thing <thing_id> <user_token>
List Profiles by Group#
mainfluxlabs-cli groups profiles <group_id> <user_token>
View Group by Profile#
mainfluxlabs-cli groups profile <profile_id> <user_token>
Group Memberships#
Create Group Memberships#
mainfluxlabs-cli group_memberships create '[{"member_id":"<member_id>","role":"<role>"}]' <group_id> <user_token>
Remove Group Memberships#
mainfluxlabs-cli group_memberships delete '["<member_id>"]' <group_id> <user_token>
Update Group Memberships#
mainfluxlabs-cli group_memberships update '[{"member_id":"<member_id>","role":"<new_role>"}]' <group_id> <user_token>
List Group Memberships#
mainfluxlabs-cli group_memberships get <group_id> <user_token>
Orgs#
Create Org#
mainfluxlabs-cli orgs create '{"name":"<org_name>","description":"<description>","metadata":{"key":"value",...}}' <user_token>
Get Org by ID#
mainfluxlabs-cli orgs get <org_id> <user_token>
List all Orgs#
mainfluxlabs-cli orgs get all <user_token>
Update Org#
mainfluxlabs-cli orgs update '{"name":"<new_name>"}' <org_id> <user_token>
Delete Org#
mainfluxlabs-cli orgs delete <org_id> <user_token>
Org Memberships#
Create Org Memberships#
mainfluxlabs-cli org_memberships create '[{"email":"<email>","role":"<role>"}]' <org_id> <user_token>
Remove Org Memberships#
mainfluxlabs-cli org_memberships delete '["<member_id>"]' <org_id> <user_token>
Get Org Membership(s)#
mainfluxlabs-cli org_memberships get <all | member_id> <org_id> <user_token>
Update Org Memberships#
mainfluxlabs-cli org_memberships update '[{"email":"<email>","role":"<new_role>"}]' <org_id> <user_token>
Webhooks#
Create Webhooks#
mainfluxlabs-cli webhooks create '[{"name":"<webhook_name>","url":"<http://webhook-url.com>","headers":{"key":"value",...}}]' <thing_id> <user_token>
Get Webhook by ID#
mainfluxlabs-cli webhooks get by-id <id> <user_token>
Get Webhooks by Group#
mainfluxlabs-cli webhooks get by-group <group_id> <user_token>
Get Webhooks by Thing#
mainfluxlabs-cli webhooks get by-thing <thing_id> <user_token>
Update Webhook#
mainfluxlabs-cli webhooks update '{"name":"<new_name>","url":"<http://webhook-url.com>"}' <webhook_id> <user_token>
Delete Webhooks#
mainfluxlabs-cli webhooks delete '["<webhook_id>"]' <user_token>
Keys management#
Issue a new Key#
mainfluxlabs-cli keys issue <duration> <user_token>
Remove API key from database#
mainfluxlabs-cli keys revoke <key_id> <user_token>
Retrieve API key with given id#
mainfluxlabs-cli keys retrieve <key_id> <user_token>
Benchmark#
Provision orgs, groups, transformer profiles, and things, then publish backfilled JSON messages over the HTTP adapter to load-test ingestion and storage.
Run a benchmark#
mainfluxlabs-cli benchmark <username> <password>
Run against a remote deployment#
The --url flag derives every service URL from the deployment base URL.
mainfluxlabs-cli benchmark <username> <password> --url https://example.com
Flags#
| Flag | Default | Description |
|---|---|---|
--orgs |
3 |
Number of orgs to create |
--groups |
5 |
Number of groups per org |
--things |
100 |
Number of things to create |
--messages |
10000 |
Number of messages to send |
--rate |
0 |
Approximate maximum messages per second, 0 disables the limit |
--batch |
100 |
Maximum number of messages per HTTP request |
--backfill-days |
30 |
Spread message timestamps over the last N days |
--mtls |
false |
Authenticate a random half of the things over mutual TLS |
Each request carries a random number of messages between 1 and --batch, sent as a JSON object for a single message and as a JSON array otherwise. The sustained ingestion rate is bounded by the database insert rate, so --rate should be set to that rate to prevent the message broker buffer from overflowing.
With --mtls, the selected things obtain client certificates from the certs service and publish over HTTPS without the Authorization header, so the deployment must verify client certificates and still accept the header from the remaining things.
The results report the requests and messages per authentication type and the failed requests per cause.