Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sysinfo journalctl action to Sansshell #279

Closed
wants to merge 4 commits into from

Conversation

sfc-gh-pchu
Copy link
Collaborator

@sfc-gh-pchu sfc-gh-pchu commented Jul 28, 2023

Description

Add journalctl action to Sansshell to read messages from journald

  • input time for since and until should follow the same pattern as "YYYY-MM-DD HH:MM:SS", and in local timezone based on where the sanssh client is run
  • the output timestamp is also in local timezone where the sanssh client is run

Note

Sansshell server will fetch the journal entries through package: github.com/coreos/go-systemd/v22 which will call systemd C API through cgo. So in order to successfully run build, pre-commit and golangci-lint. Below command need to be run to install systemd development package.

// for linux systems with apt package manager
sudo apt-get install -y libsystemd-dev
// for linux systems with yum package manager
sudo yum install systemd-devel

Usage

journalCtl [--since|--S=X] [--until|-U=X] [-tail=X] [-u|-unit=X] [-o|--output=X] [-x] :
        Get the log entries stored in journald by systemd-journald.service 
  -S string
        Sets the date (YYYY-MM-DD HH:MM:SS) we want to filter from (the date time is included)
  -U string
        Sets the date (YYYY-MM-DD HH:MM:SS) we want to filter until
  -output string
        Sets the format of the journal entries that will be shown. Right now only json and json-pretty are supported.
  -since string
        Sets the date (YYYY-MM-DD HH:MM:SS) we want to filter from
  -tail int
        If positive, the latest n records to fetch. By default, fetch latest 100 records. If negative, fetch all records (default 100)
  -unit string
        Sets systemd unit to filter messages
  -until string
        Sets the date (YYYY-MM-DD HH:MM:SS) we want to filter until (the date time is not included)
  -x    If true, augment log lines with explanatory texts from the message catalog.

Example

./sanssh --targets=localhost sysinfo journalctl -tail 5 -x
[2023-07-28 02:09:00.038149 +0000 UTC]  SDP_DevVM-pchu python3[1504]: Jul 28 02:09:00 1504 INFO] Sleeping
[2023-07-28 02:10:00.086197 +0000 UTC]  SDP_DevVM-pchu python3[1504]: Jul 28 02:10:00 1504 INFO] Sleeping
[2023-07-28 02:10:01.4509 +0000 UTC]  SDP_DevVM-pchu systemd[1]: Started Session 2968 of user root.
-- Subject: Unit session-2968.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit session-2968.scope has finished starting up.
-- 
-- The start-up result is done.
[2023-07-28 02:10:01.45659 +0000 UTC]  SDP_DevVM-pchu CROND[29297]: (root) CMD (/usr/lib64/sa/sa1 1 1)
[2023-07-28 02:11:00.080698 +0000 UTC]  SDP_DevVM-pchu python3[1504]: Jul 28 02:11:00 1504 INFO] Sleeping

@sfc-gh-pchu sfc-gh-pchu marked this pull request as ready for review July 28, 2023 23:18
@sfc-gh-srhodes
Copy link
Collaborator

Hmm, is this the first C library we're using? What happens if you try to cross-compile across architectures or compile for linux from macos?

@sfc-gh-pchu
Copy link
Collaborator Author

Yes, I think it is the first time we are using C library. Crossing compile linux from macos through GOOS=linux will always show this error message: pchu@XGX9120JFR sansshell-server % GOOS=linux go build . github.com/coreos/go-systemd/v22/internal/dlopen: build constraints exclude all Go files in /XXXXX/go/pkg/mod/github.com/coreos/go-systemd/[email protected]/internal/dlopen

And in devVM(linux env), if we directly compile it, another error will come out: # github.com/coreos/go-systemd/v22/sdjournal /Users/pchu/go/pkg/mod/github.com/coreos/go-systemd/[email protected]/sdjournal/journal.go:27:11: fatal error: 'systemd/sd-journal.h' file not found #include <systemd/sd-journal.h> ^~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
It is easy to reproduce, here is the previous merge-gate failure: https://github.com/Snowflake-Labs/sansshell/actions/runs/5694246008.

The reason is that Go compiler is unable to find the systemd/sd-journal.h file while trying to compile the github.com/coreos/go-systemd/v22/sdjournal package. This file is part of the systemd library, which provides an API for interacting with the systemd journal.

The resolution is to install the development package for systemd utility library like systemd-devel or libsystemd-dev.
Then building the project in linux system(container) won't have any issue.

But I don't have a chance to cross-compile across architecture, let me test it.

@sfc-gh-srhodes
Copy link
Collaborator

This is a pretty big bump in difficulty in compiling sansshell. I'm not convinced the benefits of this are worth the cost. We're jumping from "all you need is a go compiler if you're not touching protos" to "you need to install a non-default c library".

Is there no pure go api for this?

@sfc-gh-pchu
Copy link
Collaborator Author

I understand it increases the complexity in compiling sansshell when introducing C package. I import sdjournal for following reasons:

But let me think other workaround.

@sfc-gh-elinardi
Copy link
Collaborator

Closing this in favor of #293

@sfc-gh-elinardi sfc-gh-elinardi deleted the pchu-add-journalctl branch September 11, 2023 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants