Sync Linux Command Log to GCP
Scenario
Use GCP Ops Agent to synchronize Linux command logs to GCP Log for retention
Process and Description
- Configure Linux syslog to record command functionality, retaining all command line records in the shell
- Install GCP Ops Agent on Linux, ensuring the service account has Log Writer permissions
- Modify Ops Agent settings to forward Linux command logs to GCP
Execution Steps
Configure Linux syslog
- Set up default profile
1whoami="$(whoami)@$(echo $SSH_CONNECTION | awk '{print $1}')"2export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$whoami [$]: "${PWD/#$HOME/~}" $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"'- Configure rsyslog
1local6.* /var/log/commands.log- Restart the service after configuration
1sudo systemctl restart rsyslog- Configure logrotate
1/var/log/commands.log2{3 rotate 44 weekly5 missingok6 notifempty7 compress8 delaycompress9 sharedscripts10 postrotate11 /usr/lib/rsyslog/rsyslog-rotate12 endscript13}- Test logrotate configuration
1sudo logrotate -d /etc/logrotate.confInstall GCP Ops Agent
- Ensure the GCE service account has Logs Writer permissions
- Install Ops Agent
1curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh2sudo bash add-google-cloud-ops-agent-repo.sh --also-install- Configure Ops Agent after installation You can add other receivers to GCP Logging based on other requirements
1logging:2 receivers:3 syslog:4 type: files5 include_paths:6 - /var/log/messages7 - /var/log/syslog8 user_command:9 type: files10 include_paths:11 - /var/log/commands.log12 service:13 pipelines:14 default_pipeline:15 receivers: [syslog, user_command]- Restart ops agent service
1sudo service google-cloud-ops-agent restart