koron / night

Filtered (grep, head and tail) file HTTP server.
7 stars 0 forks source link

Night - file filter (grep, head & tail) - server.

DevOps helper HTTP server.

Install

$ npm install koron/night

   or

$ npm install -g koron/night

Tutorial

Start night on a server.

$ night

Then access by HTTP from client.

$ curl http://127.0.0.1:9280/files/var/log/messages?tail

You will get last 10 lines of /var/log/messages on a server. When open this URL using WEB browser, you will see same content, but refresh in each 5 seconds.

http://127.0.0.1:9280/files/var/log/messages?tail&refresh=5

Sever Usage

$ node [OPTIONS]

Where OPTIONS are:

Server configuration file.

Configuration file is JSON format. All properties are optional.

Example #1:

{
  "port": 8000,
  "locations": [
    "/var/log/",
    "/etc/"
  ],
  "forbiddens": [
    "/etc/ssh",
    "/etc/passwd"
  ],
  "users": [
    "test:1234",
    "root:9999"
  ]
  "commands": {
    "df": ["df", "-h"],
    "lstmp": ["ls", "-l", "/tmp"]
  }
}

Example #2: To enable SSL.

{
  "ssl": true,
  "ssl_key": "/path/to/ssl_key",
  "ssl_cert": "/path/to/ssl_cert"
}

Client Usage

Access URL is consisted from these rule.

http://{host}:{port}/files{/path/to/source}[?{filters}]

Directory Source

When /path/to/source point a directory, you will get lines in this format:

{filename}\t{type}\t{size}\t{time}

Where \t is TAB character, so you can choose by cut filter easily.

Example: get name and size entries in /var/log.

http://127.0.0.1:9280/files/var/log?cut=list:1,3

File Source

*.gz files are decompressed automatically.

*.bz2 files are decompressed automatically when available bzip2 command.

*.lz4 files are decompressed automatically when available lz4 command.

Glob Files Source

Allowed file globs (wildcards) in /path/to/source like this:

Command Source

以下のURLにアクセスすると、予め Configuration file で指定したコマンドを実行しそ の標準出力を取得できます。出力内容にはファイルなどと同じようにフィルタを適用で きます。

http://{host}:{port}/commands/{command_name}[?{filters}]

TODO: translate to English.

TODO: write description and examples.

Config Source

This feature is enabled when config_visible is true.

When access below URL, you'll get config in JSON format.

http://{host}:{port}/config[?{filters}]

Help Source

When access below URL, you'll get help (this file).

http://{host}:{port}/help[?{filters}]

Version Source

When access below URL, you'll get version info like night/0.2.1.

http://{host}:{port}/version[?{filters}]

Filter Spec

Where {filters} is:

{filter}[&{filter}...]

Where {filter} is:

{filter_name}[={options}]

Where {options} is:

{option_name}:{value}[;{option_name}:{value}...]

See other section for detail of each filters.

Example: get last 50 lines except empty lines.

http://127.0.0.1:9280/files/var/log/messages?grep=re:^$;match:false&tail=limit:50

Filters

Currenly support these filters:

Grep filter

Output lines which matches against regular expression.

As default, matching is made for whole line. But when valid option field is given, then matching is made for specified a field, which is splitted by delim character.

grep command equivalent.

Head filter

Output the first N lines.

head command equivalent.

Tail filter

Output the last N lines.

tail command equivalent.

Cut filter

Output selected fields of lines.

cut command equivalent.

Hash filter

Output hash value.

Count filter

Count lines.

LTSV filter

Output, match to value of specified label, and output selected labels.

Refresh (pseudo) filter

Add "Refresh" header with specified time (sec).

Example: Open below URL using WEB browser, it refresh in each 5 seconds automatically.

http://127.0.0.1:9280/files/var/log/messages?tail&refresh=5

All (dummy) filter

Through all lines as is. This would be used for default_filters enabled environment only.

Default filters

When you specify default_filters in config JSON, night apply that filters as default, when not specified any filters by query string. For its format, please refer Filter Spec section.

To just disable this default_filters temporary, you can use all special filter in query string.

Configuration Example

This configuration will apply "tail" filter (default: last 10 lines) as default_filters.

{
  "default_filters": "tail"
}

If you changed like below, it show last 20 lines as default_filters.

{
  "default_filters": "tail=limit:20"
}

Complex default filters

If giving a string for default_filters, night apply it to resources under /files/ only. But if you give an object, you can specify path which be applied to. Let's see configuration sample:

{
  "default_filters": {
    "/files/var/": "tail",
    "/files/tmp/": "head"
  }
}

This apply tail filter to resources under /files/var/, and head filter to under /files/tmp/.

Examples

Detect I/O error from /var/log/messages

$ curl http://127.0.0.1:9280/files/var/log/messages?grep=re:I%27O

Check whether a program "WATCHDOG" was executed by cron or not.

$ curl http://127.0.0.1:9280/files/var/log/cron?grep=re:WATCHDOG

Found errors of HTTP access, limited recent 100 records.

$ curl http://127.0.0.1:9280/files/var/log/nginx/access.log?grep=re:200,match:false&tail=limit:100