Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[grafana] conf and logs from snap added #3289

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions sos/report/plugins/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,53 @@ class Grafana(Plugin, IndependentPlugin):

packages = ('grafana',)

def _is_snap_installed(self):
grafana_pkg = self.policy.package_manager.pkg_by_name('grafana')
if grafana_pkg:
return grafana_pkg['pkg_manager'] == 'snap'
return False

def setup(self):
if self.get_option("all_logs"):
self.add_copy_spec("/var/log/grafana/*.log*")
self._is_snap = self._is_snap_installed()
if self._is_snap:
grafana_cli = "grafana.grafana-cli"
log_path = "/var/snap/grafana/common/data/log/"
config_path = "/var/snap/grafana/current/conf/grafana.ini"

else:
self.add_copy_spec("/var/log/grafana/*.log")
grafana_cli = "grafana-cli"
log_path = "/var/log/grafana/"
config_path = "/etc/grafana/"

self.add_cmd_output([
"grafana-cli plugins ls",
"grafana-cli plugins list-remote",
"grafana-cli -v",
"grafana-server -v",
])
add_cmds = [
f'{grafana_cli} plugins ls',
f'{grafana_cli} plugins list-remote',
'snap info grafana' if self._is_snap else None,
f'{grafana_cli} -v' if not self._is_snap else None,
'grafana-server -v' if not self._is_snap else None
Comment on lines +44 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these commands not work with a snap installation? Or are they not present. Non-present commands are handled gracefully by Plugin, so we don't need to set these to None and filter later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise we don't need to gate the snap info command, we can just tuck an add_cmd_output() under the conditional on line 30.

]
self.add_cmd_output(list(filter(None, add_cmds)))

log_file_pattern = "*.log*" if self.get_option("all_logs") else "*.log"

self.add_copy_spec([
"/etc/grafana/",
"/etc/sysconfig/grafana-server",
log_path + log_file_pattern,
config_path,
"/etc/sysconfig/grafana-server" if not self._is_snap else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_copy_spec() also handles non-existing paths gracefully, so we can just have it listed here and it will be ignored for snap installations.

])

def postproc(self):
protect_keys = [
"admin_password", "secret_key"
"admin_password",
"secret_key",
"password",
"client_secret"
Comment on lines -42 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, that helps also to non-snap instances.

]
inifile = (
"/var/snap/grafana/current/conf/grafana.ini"
if self._is_snap
else "/etc/grafana/grafana.ini"
)

regexp = r"(^\s*(%s)\s*=\s*)(.*)" % "|".join(protect_keys)
self.do_path_regex_sub("/etc/grafana/grafana.ini",
regexp, r"\1*********")
regexp = r"((?m)^\s*(%s)\s*=\s*)(.*)" % "|".join(protect_keys)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove the (?m) as that is now implied by default in the self.do_path_regex_sub() based on issue #3301 and not going to be allowed in this manner in py311, thanks

self.do_path_regex_sub(inifile, regexp, r"\1*********")