Skip to content

Commit

Permalink
support docker discover
Browse files Browse the repository at this point in the history
  • Loading branch information
arstercz committed Mar 1, 2023
1 parent ce6a6d0 commit 600fb1f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
14 changes: 14 additions & 0 deletions scripts/deb/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ fi

fi

if pidof dockerd >/dev/null 2>&1; then
if getent group docker >/dev/null 2>&1; then
usermod -aG docker telegraf
else
if [[ -e "/var/run/docker.sock" ]]; then
setfacl -m g:telegraf:rw /var/run/docker.sock
else
echo "Warn - no docker user or setfacl error"
echo "Warn - need add telegraf user to read docker unix group(eg: /var/run/docker.sock)"
echo
fi
fi
fi

if [[ "$(readlink /proc/1/exe)" == */systemd ]]; then
install_systemd /lib/systemd/system/telegraf.service
deb-systemd-invoke restart telegraf.service || echo "WARNING: systemd not running."
Expand Down
14 changes: 14 additions & 0 deletions scripts/rpm/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ fi
chmod 440 /etc/sudoers.d/telegraf
fi

if pidof dockerd >/dev/null 2>&1; then
if getent group docker >/dev/null 2>&1; then
usermod -aG docker telegraf
else
if [[ -e "/var/run/docker.sock" ]]; then
setfacl -m g:telegraf:rw /var/run/docker.sock
else
echo "Warn - no docker user or setfacl error"
echo "Warn - need add telegraf user to read docker unix group(eg: /var/run/docker.sock)"
echo
fi
fi
fi

# Distribution-specific logic
if [[ -f /etc/redhat-release ]] || [[ -f /etc/SuSE-release ]]; then
# RHEL-variant logic
Expand Down
68 changes: 66 additions & 2 deletions scripts/telegraf-discover
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ sub is_support {
my $self = shift;
my $type = shift;

my @lists = qw(memcached redis pika mysql mongodb postgresql dns_query megacli iptables haproxy);
my @lists = qw(memcached redis pika mysql mongodb postgresql dns_query megacli iptables docker haproxy);
foreach my $k (@lists) {
return 1 if $k eq $type;
}
Expand Down Expand Up @@ -1810,10 +1810,73 @@ sub get_server_list {
$results{non_listen}->{dns_query} = {};
$results{one_enable}->{megacli} = {};
$results{one_enable}->{iptables} = {};
$results{one_enable}->{docker} = {};

return \%results;
}

sub _replace_docker {
my $temp = shift;
my $sockf = shift;

if (-e $sockf) {
$temp =~ s/docker_sock_path/$sockf/g;
}
else {
$temp = "";
}

return $temp;
}

sub disc_docker {
my $self = shift;
my $secs = shift;

my $temp = <<'TEMP_END';
# Read metrics about docker containers
[[inputs.docker]]
## Docker Endpoint
## To use TCP, set endpoint = "tcp://[ip]:[port]"
## To use environment variables (ie, docker-machine), set endpoint = "ENV"
endpoint = "docker_sock_path"
## Set to true to collect Swarm metrics(desired_replicas, running_replicas)
gather_services = false
## Set the source tag for the metrics to the container ID hostname, eg first 12 chars
source_tag = true
## Containers to include and exclude. Collect all if empty. Globs accepted.
container_name_include = []
container_name_exclude = []
## Container states to include and exclude. Globs accepted.
## When empty only containers in the "running" state will be captured.
## example: container_state_include = ["created", "restarting", "running", "removing", "paused", "exited", "dead"]
## example: container_state_exclude = ["created", "restarting", "running", "removing", "paused", "exited", "dead"]
container_state_include = ["created", "restarting", "running", "removing", "paused", "exited", "dead"]
# container_state_exclude = []
## Timeout for docker list, info, and stats commands
timeout = "5s"
perdevice = false
perdevice_include = ["blkio", "network"]
total = true
total_include = ["cpu", "blkio", "network"]
## docker labels to include and exclude as tags. Globs accepted.
docker_label_include = []
docker_label_exclude = []
## Which environment variables should we use as a tag
tag_env = ["JAVA_HOME", "HEAP_SIZE"]
TEMP_END

return _replace_docker($temp, "/var/run/docker.sock")
}

sub disc_iptables {
my $self = shift;
my $secs = shift;
Expand Down Expand Up @@ -2633,6 +2696,7 @@ sub temp_outs {

$select_func{'megacli'} = $self->disc_megacli();
$select_func{'iptables'} = $self->disc_iptables();
$select_func{'docker'} = $self->disc_docker();

return $select_func{$type};
}
Expand Down Expand Up @@ -2848,7 +2912,7 @@ sub gen_conf_for_one_enable {

my $counter = 0;

$secs->{ignore} ||= 0 if _one_enable_white($type);
$secs->{ignore} = _one_enable_white($type);

if (defined($secs->{ignore})) {
if ($secs->{ignore} > 0) {
Expand Down

0 comments on commit 600fb1f

Please sign in to comment.