Skip to content

Managing connection as parameter #74

@HPF97

Description

@HPF97

Is your feature request related to a problem? Please describe.
Pass the connection from the object to the parameter.
By this, same code could be used for different connections.

Describe the solution you'd like
Up-to-now, as the connection brings the exec_command or exec_command_line, it is not possible to use the same query for different connections.
Code have to be duplicated
I see 2 solutions for the connection being passed as parameter the xth here) :

  • the connection is a parameter to exec_command and so exec_command($x)
  • allow the parameter to call the exec_command so $x.exec_command

Describe alternatives you've considered
Make a big query with a main "case" is possible but query code is still duplicated.
Making a case in the from, is not supported by SQL.
Up-to-now : only code duplication is possible, one file/query per connection.

Additional context
Let assume that we have 2 groups of servers : the first group "u20" is under Ubuntu 20, the second "u24" under Ubuntu 24. On each one we have installed ClamAV, but due to their version, only ClamAV v1.0.6 is allowed for u20 servers, whereas ClamAV v1.4.2 car be installed on u24.
Now we want to make a dashboard to check that the correct version of ClamAV is installed on each server.
I would have to:

  • Define in the exec configuration file a connection for each VM access and an connection aggregation for each group
  • Make a first query file with the test for u20
query "check_u20_package" {
  sql = <<-EOQ
  with 
    mes_package as (
      select
          _ctx ->> 'connection_name' as vm
        , split_part(line, ',', 1) as package
        , split_part(line, ',', 2) as version
        , line
      from
        u20.exec_command_line  
      where
        command = 'dpkg-query -W -f=''$${Package},$${Version}\n''' 
      order by
        vm
      , package
      )

  select mp.vm as resource, 'ok' as status, mp.vm || ' : Correct version for ' || $1 as reason 
    from mes_package as mp 
    where mp.package = $1 and strpos(version, $2) = 1
  union 
    select mp.vm as resource, 'alarm' as status, mp.vm || ' : Bad version for ' || $1 as reason 
    from mes_package as mp 
    where mp.package = $1 and strpos(version, $2) = 0
  union
    select mp.vm as resource, 'error' as status, mp.vm || ' : Missing package : ' || $1 as reason
    from mes_package as mp 
    where not exists (
      select 
        mp.vm as vm
      from
        mes_package as mc
      where 
        mc.vm = mp.vm and mc.package = $1
      );
  EOQ
  param "package_name" {
    description = "1. Name of the package"
  }
  param "package_version" {
    description = "2. Version of the package"
  }
}

the corresponding control would be

control "clamav_linux_u20_package_version" {
  title = "Control ClamAV on Ubuntu20 OS - Package and version"
  description = "Checking that ClamAV is deployed with the correction version on Ubuntu20 OS."
  query = query.check_u20_package
  args = {
    "package_name"    = "clamav"
    "package_version" = "1.0.6"
  }
}

and I would have to duplicate the query file, to get exactly the same thing....
Only one line would be different : u24.exec_command_line instead of u20.exec_command_line.

It would be better to get

control "clamav_linux_u20_package_version" {
  title = "Control ClamAV on Ubuntu20 OS - Package and version"
  description = "Checking that ClamAV is deployed with the correction version on Ubuntu20 OS."
  query = query.check_**ubuntu**_package
  args = {
    "package_name"    = "clamav"
    "package_version" = "1.0.6"
    "connection_name" = "u20"
  }
}

and only one query file

query "check_u20_package" {
  sql = <<-EOQ
  with 
    mes_package as (
      select
          _ctx ->> 'connection_name' as vm
        , split_part(line, ',', 1) as package
        , split_part(line, ',', 2) as version
        , line
      from
        **exec_command_line($3)**
      where
        command = 'dpkg-query -W -f=''$${Package},$${Version}\n''' 
      order by
        vm
      , package
      )

  select mp.vm as resource, 'ok' as status, mp.vm || ' : Correct version for ' || $1 as reason 
    from mes_package as mp 
    where mp.package = $1 and strpos(version, $2) = 1
  union 
    select mp.vm as resource, 'alarm' as status, mp.vm || ' : Bad version for ' || $1 as reason 
    from mes_package as mp 
    where mp.package = $1 and strpos(version, $2) = 0
  union
    select mp.vm as resource, 'error' as status, mp.vm || ' : Missing package : ' || $1 as reason
    from mes_package as mp 
    where not exists (
      select 
        mp.vm as vm
      from
        mes_package as mc
      where 
        mc.vm = mp.vm and mc.package = $1
      );
  EOQ
  param "package_name" {
    description = "1. Name of the package"
  }
  param "package_version" {
    description = "2. Version of the package"
  }
  param "connection_name" {
    description = "3. Name of the connection"
  }
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requeststaleNo recent activity has been detected on this issue/PR and it will be closed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions