Set up and manage sensuctl

Sensuctl is a command line tool for managing resources within Sensu. It works by calling Sensu’s underlying API to create, read, update, and delete resources, events, and entities. Sensuctl is available for Linux, macOS, and Windows. See Install Sensu to install and configure sensuctl.

First-time setup

To set up sensuctl, run sensuctl configure to log in to sensuctl and connect to the Sensu backend:

sensuctl configure

When prompted, type the Sensu backend URL and your Sensu access credentials.

? Sensu Backend URL: http://127.0.0.1:8080
? Username: YOUR_USERNAME
? Password: YOUR_PASSWORD
? Namespace: default
? Preferred output format: tabular

Sensu backend URL

The Sensu backend URL is the HTTP or HTTPS URL where sensuctl can connect to the Sensu backend server. The default URL is http://127.0.0.1:8080.

To connect to a Sensu cluster, connect sensuctl to any single backend in the cluster. For information about configuring the Sensu backend URL, see the backend reference.

Configuration files

During configuration, sensuctl creates configuration files that contain information for connecting to your Sensu Go deployment. You can find these files at $HOME/.config/sensu/sensuctl/profile and $HOME/.config/sensu/sensuctl/cluster.

For example:

cat .config/sensu/sensuctl/profile
{
  "format": "tabular",
  "namespace": "demo",
  "username": "admin"
}
cat .config/sensu/sensuctl/cluster 
{
  "api-url": "http://localhost:8080",
  "trusted-ca-file": "",
  "insecure-skip-tls-verify": false,
  "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "expires_at": 1550082282,
  "refresh_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

These configuration files are useful if you want to know which cluster you’re connecting to or which namespace or username you’re currently configured to use.

Username, password, and namespace

During the Sensu backend installation process, you create an administrator username and password and a default namespace.

Your ability to get, list, create, update, and delete resources with sensuctl depends on the permissions assigned to your Sensu user. For more information about configuring Sensu access control, see the RBAC reference.

NOTE: For a new installation, you can set administrator credentials with environment variables during initialization. If you are using Docker and you do not include the environment variables to set administrator credentials, the backend will initialize with the default username (admin) and password (P@ssw0rd!).

Change admin user’s password

After you have installed and configured sensuctl, you can change the admin user’s password. Run:

sensuctl user change-password --interactive

You must specify the user’s current password to use the sensuctl user change-password command.

Reset a user password

To reset a user password without specifying the current password, run:

sensuctl user reset-password USERNAME --interactive

You must have admin permissions to use the sensuctl user reset-password command.

Test a user password

To test the password for a user created with Sensu’s built-in basic authentication:

sensuctl user test-creds USERNAME --password 'password'

An empty response indicates valid credentials. A request-unauthorized response indicates invalid credentials.

NOTE: The sensuctl user test-creds command tests passwords for users created with Sensu’s built-in basic authentication provider. It does not test user credentials defined via an authentication provider like Lightweight Directory Access Protocol (LDAP) or Active Directory (AD).

For example, if you test LDAP credentials with the sensuctl user test-creds command, the backend will log an error, even if you know the LDAP credentials are correct:

{"component":"apid.routers","error":"basic provider is disabled","level":"info","msg":"invalid username and/or password","time":"2020-02-07T20:42:14Z","user":"dev"}

Generate a password hash

To generate a password hash for a specified cleartext password, run:

sensuctl user hash-password PASSWORD

The sensuctl user hash-password command creates a bcrypt hash of the specified password. You can use this hash instead of the password when you use sensuctl to create and edit users.

Preferred output format

Sensuctl supports the following output formats:

  • tabular: A user-friendly, columnar format
  • wrapped-json: An accepted format for use with sensuctl create
  • yaml: An accepted format for use with sensuctl create
  • json: A format used by the Sensu API

After you are logged in, you can change the output format with sensuctl config set-format or set the output format per command with the --format flag.

Non-interactive mode

Run sensuctl configure non-interactively by adding the -n (--non-interactive) flag.

sensuctl configure -n --url http://127.0.0.1:8080 --username YOUR_USERNAME --password YOUR_PASSWORD --format tabular

Get help

Sensuctl supports a --help flag for each command and subcommand.

See command and global flags

sensuctl --help

See subcommands and flags

sensuctl check --help

See usage and flags

sensuctl check delete --help

Manage sensuctl

The sencutl config command lets you view the current sensuctl configuration and set the namespace and output format.

View sensuctl config

To view the active configuration for sensuctl:

sensuctl config view

The sensuctl config view response includes the Sensu backend URL, default namespace for the current user, default output format for the current user, and currently configured username:

=== Active Configuration
API URL:   http://127.0.0.1:8080
Namespace: default
Format:    tabular
Username:  admin

Set output format

Use the set-format command to change the default output format for the current user.

For example, to change the output format to tabular:

sensuctl config set-format tabular

Set namespace

Use the set-namespace command to change the default namespace for the current user. For more information about configuring Sensu access control, see the RBAC reference.

For example, to change the default namespace to development:

sensuctl config set-namespace development

Log out of sensuctl

To log out of sensuctl:

sensuctl logout

To log back in to sensuctl:

sensuctl configure

View the sensuctl version number

To display the current version of sensuctl:

sensuctl version

Global flags

Global flags modify settings specific to sensuctl, such as the Sensu backend URL and namespace. You can use global flags with most sensuctl commands.

--api-url string             host URL of Sensu installation
--cache-dir string           path to directory containing cache & temporary files
--config-dir string          path to directory containing configuration files
--insecure-skip-tls-verify   skip TLS certificate verification (not recommended!)
--namespace string           namespace in which we perform actions
--trusted-ca-file string     TLS CA certificate bundle in PEM format

You can set these flags permanently by editing .config/sensu/sensuctl/{cluster, profile}.

Shell auto-completion

Installation (Bash shell)

Make sure bash-completion is installed. If you use a current Linux in a non-minimal installation, bash-completion should be available.

On macOS, install with:

brew install bash-completion

Then add this to your ~/.bash_profile:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi

After bash-completion is installed, add this to your ~/.bash_profile:

source <(sensuctl completion bash)

Now you can source your ~/.bash_profile or launch a new terminal to use shell auto-completion.

source ~/.bash_profile

Installation (ZSH)

Add this to your ~/.zshrc:

source <(sensuctl completion zsh)

Now you can source your ~/.zshrc or launch a new terminal to use shell auto-completion.

source ~/.zshrc

Usage

sensuctl Tab

check       configure   event       user
asset       completion  entity      handler

sensuctl check Tab

create  delete  import  list