Send google compute startup script output to stackdriver
For ease of management I'd like the output of my GCE startup script to be sent to stackdriver so that admins don't need to SSH into the instance to see the output.
I found this guide for setting up Stackdriver on GCE, but it requires any logs to be manually sent by using logger
and I don't want to wrap each command in logger
The next best idea I've got is to have the first line of the file run tail /var/log/daemon.log
and pipe the output to logger.
Is there a better, more foolproof solution? I can't find anything but it seems like something that would be commonly wanted I'm surprised I'm not seeing a solution.
You can customize the Logging agent to send additional logs to Logging by adding input configurations. The detailed instructions can be found here.
-
From the Linux command prompt, create a log file:
touch /tmp/test-unstructured-log.log
-
Create a new configuration file labeled
test-unstructured-log.conf
in the additional configuration directory/etc/google-fluentd/config.d
:
sudo tee /etc/google-fluentd/config.d/test-unstructured-log.conf <<EOF
<source>
@type tail
<parse>
# 'none' indicates the log is unstructured (text).
@type none
</parse>
# The path of the log file.
path /tmp/test-unstructured-log.log
# The path of the position file that records where in the log file
# we have processed already. This is useful when the agent
# restarts.
pos_file /var/lib/google-fluentd/pos/test-unstructured-log.pos
read_from_head true
# The log tag for this log input.
tag unstructured-log
</source>
EOF
-
Restart the agent to apply the configuration changes:
sudo service google-fluentd restart
-
Generate a log record into the log file:
echo 'This is a log from the log file at test-unstructured-log.log' >> /tmp/test-unstructured-log.log
-
Check the Logs Explorer to see the ingested log entry:
{
insertId: "eps2n7g1hq99qp"
labels: {
compute.googleapis.com/resource_name: "add-unstructured-log-resource"
}
logName: "projects/my-sample-project-12345/logs/unstructured-log"
receiveTimestamp: "2018-03-21T01:47:11.475065313Z"
resource: {
labels: {
instance_id: "3914079432219560274"
project_id: "my-sample-project-12345"
zone: "us-central1-c"
}
type: "gce_instance"
}
textPayload: "This is a log from the log file at test-unstructured-log.log"
timestamp: "2018-03-21T01:47:05.051902169Z"
}