Code Monkey home page Code Monkey logo

nhi's Introduction

workflow Gitter chat Tweet

nhi is a revolutionary tool which automatically captures all potentially useful information about each executed command and everything around, and delivers powerful querying mechanism.

nhi keeps records of:

  • command
  • output of command
  • exit status of command
  • working directory at the end of command execution
  • start time of command
  • finish time of command
  • shell prompt at the time of command execution
  • (and much more in the future ๐Ÿ˜„)

nhi also keeps records of information about shell session in general.

These features allow retrievement of commands executed in past and whole shell sessions, as well as all other useful information in a convenient way.

nhi daemon is based on eBPF - a technology built into linux kernel. Usage of eBPF guarantee a great performance and low overhead of the tool, because tracing is being safely done inside kernel.

nhi does not affect behaviour of any program/process (and OS in general).

Watch the introductory video to see how nhi works in practice.

Usage

For the full documentation, read the nhi(1) man page.

For quick reference, use nhi --help flags.

Requirements

bash or zsh, x86_64 architecture, systemd and linux kernel 5.8+. Some major distributions that ship with the linux kernel 5.8+:

  • Debian 11
  • Ubuntu 20.10+
  • Fedora 33+

xterm based terminals are highly recommended. (If you don't know whether your terminal emulator is xterm based or not, it most likely is xterm based. xterm is a standard for terminal emulators.)

Installation

Ubuntu 21.10+

Step 1: Ubuntu has oddly compiled bash and zsh binaries which are missing some data required by nhi. To install shells that are compiled "normally", like on every other distro run:

sudo apt-get remove zsh-common
sudo add-apt-repository ppa:strang1ato/default-bash-and-zsh
sudo apt-get update
sudo apt-get install --reinstall bash

and if you were using zsh:

sudo apt-get install zsh

From now bash and zsh will be upgraded/installed only from the newly added repository.

Step 2: Run:

sudo add-apt-repository ppa:strang1ato/nhi
sudo apt-get update
sudo apt-get install nhi

Step 3: Add to the end of your .bashrc:

source /etc/nhi/nhi.bash

And if you use zsh add to the end of your .zshrc:

source /etc/nhi/nhi.zsh

Step 4: Restart your computer.

Other distributions

Step 1: Install objdump, awk, sqlite3, libsqlite3-dev and libbpf-dev (example for linux debian systems):

sudo apt-get install binutils gawk sqlite3 libsqlite3-dev libbpf-dev

Step 2: Download all seven files from the latest release, and put them in a new empty directory.

Step 3: Go to the new directory and run:

sudo -E bash ./install

Step 4: Restart your computer.

Testing

In order to check if you installed nhi succesfully open new terminal window and execute for example:

echo nhi test

and

nhi fetch {-1}

If you see "nhi test" once again, it means that nhi is properly installed.

Contributions

Contributions are welcome! Any tips and suggestions are appreciated. If you found any bug feel free to submit a new issue.

nhi's People

Contributors

strang1ato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nhi's Issues

`nhi` doesn't record commands

Installed from release 0.2.1, nhi fetch shows only the creation of sessions, no commands have been logged, nor the finish of sessions. Same result from within and without tmux

$ uname -a
Linux Aostro-5468 5.14.16-arch1-1 #1 SMP PREEMPT Tue, 02 Nov 2021 22:22:59 +0000 x86_64 GNU/Linux

$ zsh --version
zsh 5.8 (x86_64-pc-linux-gnu)

Debian 11 problem

when i run
nhi fetch {-1}
i get
no such shell session: 0
source /etc/nhi/nhi.bash exists in ~/.bashrc , i am running everything as root
what could be wrong? Also where is the history of commands stored?
no log file in /tmp

Issue on arch

I'm running ZSH on Arch, I've installed nhi but I have errors in the logs.

2024-07-03 16:22:13 | process_vm_readv failed at get_shell_environ
2024-07-03 16:22:13 | no such table: 0
2024-07-03 16:22:18 | process_vm_readv failed at get_shell_environ
2024-07-03 16:22:18 | no such table: 0
2024-07-03 16:22:24 | process_vm_readv failed at get_shell_environ
2024-07-03 16:22:24 | no such table: 0
2024-07-03 16:22:50 | process_vm_readv failed at get_shell_environ
2024-07-03 16:22:50 | no such table: 0
2024-07-03 16:22:52 | process_vm_readv failed at get_shell_environ
2024-07-03 16:22:52 | no such table: 0
  1. Install gave me this output :

    $ sudo -E bash ./install
    wal

    I don't know why it spit this "wal" string.

  2. Here is the result of getent group tty : tty:x:5:

I've looked into the get_shell_environ code, and these 2 function do not return anything :

objdump -T $(which bash) | awk -v sym=environ ' $NF == sym && $4 == ".bss"  { print $1; exit }'
objdump -T $(which zsh) | awk -v sym=environ ' $NF == sym && $4 ==".bss"  { print $1; exit }'

Digging deeper, there is indeed no match :

$ objdump -T $(which bash) | grep environ
0000000000000000      DO *UND*  0000000000000000 (GLIBC_2.2.5) environ
0000000000000000      DO *UND*  0000000000000000 (GLIBC_2.2.5) __environ
000000000010ff40 g    DO .bss   0000000000000008  Base        shell_environment
000000000010fe2c g    DO .bss   0000000000000004  Base        subshell_environment
0000000000111660 g    DO .bss   0000000000000004  Base        assigning_in_environment
$ objdump -T $(which zsh) | grep environ
0000000000000000      DO *UND*  0000000000000000 (GLIBC_2.2.5) environ
0000000000000000      DO *UND*  0000000000000000 (GLIBC_2.2.5) __environ

How can I solve this ?

Thanks :)

Output of commands not saved when vim is ran

Hello,

Thank you, this project is really cool!

However, my commands are not logged after running commands such as vim:

vagrant@bullseye:~$ nhi fetch {-1}
vagrant@bullseye:~$ echo test
test
vagrant@bullseye:~$ vim
vagrant@bullseye:~$ echo test
(nothing here)

This issue seems to come from here:

nhi/daemon/src/nhi.bpf.c

Lines 393 to 404 in a3c65cc

} else if (write_event->output[i+7] == 'l') {
helper->omit_write = 0;
bpf_map_update_elem(&shells, &shell_index, helper, BPF_ANY);
write_event->output[i] = 0;
write_event->output[i+1] = 0;
write_event->output[i+2] = 0;
write_event->output[i+3] = 0;
write_event->output[i+4] = 0;
write_event->output[i+5] = 0;
write_event->output[i+6] = 0;
write_event->output[i+7] = 0;
}

Looking in the database (at /var/nhi/db), I can see:

echo test
b'\xfftest\n'
vim
b'\xff\x1b[?1000h\xff\x1b[34h\x1b[?25h\x00\x00\x00\x00\x00\x00\x00\x00'
echo test
b''

The last 8 bytes are set to \x00 which should mean that omit_write is set to zero, but I do not understand why it still refuses to log the next commands.

On another note, it would be nice to remove the null bytes and the rest of what was logged (\x1b[?1000h\xff\x1b[34h\x1b[?25h), it messes up my terminal after executing nhi fetch (which makes me have to execute reset to clean my terminal).

Move to BPF magic

I am starting to moving from ptrace to BPF

BPF will take over nhi

`nhid` segfaults on 0.2.2 while using tmux

Not really an issue (probably because I haven't used nhi that much), but I saw this in the kernel messages:

[  +0.000004] nhid[3381461]: segfault at 0 ip 00007fed21d1f515 sp 00007ffd5ca83e38 error 4 in libc-2.33.so[7fed21be5000+14b000]
[  +0.000010] Code: 00 00 0f 1f 00 31 c0 c5 f8 77 c3 66 2e 0f 1f 84 00 00 00 00 00 f3 0f 1e fa 89 f9 48 89 fa c5 f9 ef c0 83 e1 3f 83 f9 20 77 2b <c5> fd 74 0f c5 fd d7 c1 85 c0 0f 85 eb 00 00 00 48 83 c7 20 83 e1

I don't know why there isn't a coredump generated, but it happens inside tmux and with my .zshrc (haven't tested other conditions yet). Again, not a urgent problem for me, just something for you to look into

Update bash package in default-bash-and-zsh ppa with /etc/bash.bashrc support

On Ubuntu, while using nhi after following the install procedures outlined in the README.md, I noticed that my system-wide /etc/bash.bashrc was not getting sourced into my interactive shells (it still worked with login shells as /etc/profile loads the /etc/bash.bashrc). After digging into the issue, it seems like the issue starts after reinstalling the bash package from ppa:strang1ato/default-bash-and-zsh. After some Googling it looks like the issue might be due to a missing option (-DSYS_BASHRC) when bash is being compiled.

Simplified steps to reproduce on a clean Ubuntu 22.04 instance...

# ssh to the instance

sudo apt update
sudo apt upgrade
sudo bash -c 'echo "echo \"Hit: /etc/bash.bashrc\"" >> /etc/bash.bashrc'
echo "echo \"Hit: ~/.bashrc\"" >> ~/.bashrc

# exit and ssh back in to the instance, note you get two echos

bash

# note you get two echos

# next we will exit the interactive shell and go back to the login shell from ssh
exit

sudo apt remove zsh-common
sudo add-apt-repository ppa:strang1ato/default-bash-and-zsh
sudo apt update
sudo apt install --reinstall bash

# exit and ssh back in to the instance, note you get two echos

bash

# note you only get one echo

Thank you for this project and work!

no such shell session: 0

After following the install steps for other distributions I did run the tests commands to know if everything was good and got the following message after running nhi fetch;

$ nhi fetch {-1}
no such shell session: 0

Im using Debian bullseye and bash

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.