Для удобства управления я хотел бы, чтобы вывод сценария запуска GCE отправлялся в stackdriver, чтобы администраторам не нужно было подключаться к экземпляру по SSH для просмотра вывода.
Я нашел это руководство по настройке Stackdriver на GCE, но оно требует, чтобы любые журналы отправлялись вручную с помощью logger
, а я не хочу обертывать каждую команду в logger
Следующая лучшая идея, которая у меня есть, - это чтобы первая строка файла выполнялась tail /var/log/daemon.log
, а вывод передавался в logger.
Есть ли лучшее, более надежное решение? Я ничего не могу найти, но это похоже на то, что обычно требуется, и я удивлен, что не вижу решения.
Вы можете настроить агент Logging для отправки дополнительных журналов в Logging, добавив входные конфигурации. Подробные инструкции можно найти здесь.
В командной строке Linux создайте файл журнала:
touch /tmp/test-unstructured-log.log
Создайте новый файл конфигурации с именем test-unstructured-log.conf
в дополнительном каталоге конфигурации /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
Перезапустите агент для применения изменений конфигурации:
sudo service google-fluentd restart
Сгенерируйте запись журнала в файл журнала:
echo 'This is a log from the log file at test-unstructured-log.log' >> /tmp/test-unstructured-log.log
Проверьте в Logs Explorer запись журнала:
{
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"
}