Skip to content

Commit

Permalink
auto find docker socket file, and add pre_check method to check file …
Browse files Browse the repository at this point in the history
…permission
  • Loading branch information
arstercz committed Mar 7, 2023
1 parent 6ac30d9 commit a1b0b85
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
5 changes: 3 additions & 2 deletions scripts/deb/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ 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
sockf=$(netstat -axp | grep docker.sock | awk '{print $NF}' | tail -n 1)
if [[ -e "$sockf" ]]; then
setfacl -m g:telegraf:rw $sockf
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)"
Expand Down
5 changes: 3 additions & 2 deletions scripts/rpm/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,9 @@ 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
sockf=$(netstat -axp | grep docker.sock | awk '{print $NF}' | tail -n 1)
if [[ -e "$sockf" ]]; then
setfacl -m g:telegraf:rw $sockf
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)"
Expand Down
28 changes: 24 additions & 4 deletions scripts/telegraf-discover
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ else {
}

my $disconf = Disconf->new( (update => $update, force => $force_overwrite) );

# hook actions before running
$disconf->pre_check();

my $results = $disconf->get_server_list();
my $sects = Utils::parsecfg($discover);
$| = 1; # enforce flush for print
Expand Down Expand Up @@ -1671,6 +1675,14 @@ sub new {
return bless $self, $class;
}

sub pre_check {
my $self = shift;
if ($self->is_support("docker")) {
perm_docker();
}
# ......
}

sub is_support {
my $self = shift;
my $type = shift;
Expand Down Expand Up @@ -1815,8 +1827,8 @@ sub get_server_list {
return \%results;
}

sub _perm_docker {
my $sockf = shift;
sub perm_docker {
my $sockf = shift || _find_docker_sock();

my $id = getgrnam("docker");
if (defined $id and $id > 0) {
Expand All @@ -1836,6 +1848,7 @@ sub _perm_docker {
Utils::time_print("error - setfacl to $sockf error! high: $high, low: $low");
}
else {
Utils::time_print("setfacl to $sockf ok");
return 1;
}
}
Expand All @@ -1848,7 +1861,7 @@ sub _replace_docker {
my $temp = shift;
my $sockf = shift;

if (-e $sockf && _perm_docker($sockf)) {
if (-e $sockf && perm_docker($sockf)) {
my $socks = "unix://$sockf";
$temp =~ s/docker_sock_path/$socks/g;
}
Expand All @@ -1859,6 +1872,13 @@ sub _replace_docker {
return $temp;
}

sub _find_docker_sock {
my $cmd = "netstat -axp | grep docker.sock | awk '{print \$NF}' | tail -n 1";
my $res = Utils::exec_cmd_return("$cmd") || '';

return $res;
}

sub disc_docker {
my $self = shift;
my $secs = shift;
Expand Down Expand Up @@ -1904,7 +1924,7 @@ sub disc_docker {
tag_env = ["JAVA_HOME", "HEAP_SIZE"]
TEMP_END

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

sub disc_iptables {
Expand Down

0 comments on commit a1b0b85

Please sign in to comment.