[pve-devel] [PATCH manager 04/20] ceph: factor out the service info generation

Dominik Csapak d.csapak at proxmox.com
Tue Jun 4 14:47:43 CEST 2019


and include a call to $type metadata to include the version

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 PVE/API2/Ceph/MDS.pm | 21 +++------------------
 PVE/API2/Ceph/MGR.pm |  1 +
 PVE/API2/Ceph/MON.pm | 20 ++++++--------------
 PVE/Ceph/Services.pm | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 59 insertions(+), 33 deletions(-)

diff --git a/PVE/API2/Ceph/MDS.pm b/PVE/API2/Ceph/MDS.pm
index 19489690..532eb404 100644
--- a/PVE/API2/Ceph/MDS.pm
+++ b/PVE/API2/Ceph/MDS.pm
@@ -71,26 +71,11 @@ __PACKAGE__->register_method ({
 	my $res = [];
 
 	my $cfg = cfs_read_file('ceph.conf');
+	my $rados = PVE::RADOS->new();
 
-	my $mds_hash = {};
-
-	foreach my $section (keys %$cfg) {
-	    my $d = $cfg->{$section};
-
-	    if ($section =~ m/^mds\.(\S+)$/) {
-		my $mds_id = $1;
-		if (defined($d->{host})) {
-		    $mds_hash->{$mds_id} = {
-			name => $mds_id,
-			state => 'unknown',
-			addr => $d->{host},
-			host => $d->{host},
-		    };
-		}
-	    }
-	}
+	my $mds_hash = PVE::Ceph::Services::get_services_info("mds", $cfg, $rados);
 
-	my $mds_state = PVE::Ceph::Services::get_cluster_mds_state();
+	my $mds_state = PVE::Ceph::Services::get_cluster_mds_state($rados);
 	foreach my $name (keys %$mds_state) {
 	    my $d = $mds_state->{$name};
 	    # just overwrite, this always provides more info
diff --git a/PVE/API2/Ceph/MGR.pm b/PVE/API2/Ceph/MGR.pm
index 2ded684d..c1d38d7a 100644
--- a/PVE/API2/Ceph/MGR.pm
+++ b/PVE/API2/Ceph/MGR.pm
@@ -7,6 +7,7 @@ use File::Path;
 
 use PVE::Ceph::Tools;
 use PVE::Ceph::Services;
+use PVE::Cluster qw(cfs_read_file);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RADOS;
 use PVE::RPCEnvironment;
diff --git a/PVE/API2/Ceph/MON.pm b/PVE/API2/Ceph/MON.pm
index 17660039..59f30193 100644
--- a/PVE/API2/Ceph/MON.pm
+++ b/PVE/API2/Ceph/MON.pm
@@ -68,7 +68,8 @@ __PACKAGE__->register_method ({
 	    type => "object",
 	    properties => {
 		name => { type => 'string' },
-		addr => { type => 'string' },
+		addr => { type => 'string', optional => 1 },
+		host => { type => 'string', optional => 1 },
 	    },
 	},
 	links => [ { rel => 'child', href => "{name}" } ],
@@ -83,32 +84,23 @@ __PACKAGE__->register_method ({
 	my $cfg = cfs_read_file('ceph.conf');
 
 	my $monhash = {};
-	foreach my $section (keys %$cfg) {
-	    my $d = $cfg->{$section};
-	    if ($section =~ m/^mon\.(\S+)$/) {
-		my $monid = $1;
-		if ($d->{'mon addr'} && $d->{'host'}) {
-		    $monhash->{$monid} = {
-			addr => $d->{'mon addr'},
-			host => $d->{'host'},
-			name => $monid,
-		    }
-		}
-	    }
-	}
 
 	eval {
 	    my $rados = PVE::RADOS->new();
+	    $monhash = PVE::Ceph::Services::get_services_info("mon", $cfg, $rados);
 	    my $monstat = $rados->mon_command({ prefix => 'mon_status' });
+
 	    my $mons = $monstat->{monmap}->{mons};
 	    foreach my $d (@$mons) {
 		next if !defined($d->{name});
 		$monhash->{$d->{name}}->{rank} = $d->{rank};
 		$monhash->{$d->{name}}->{addr} = $d->{addr};
+		$monhash->{$d->{name}}->{state} = 'running';
 		if (grep { $_ eq $d->{rank} } @{$monstat->{quorum}}) {
 		    $monhash->{$d->{name}}->{quorum} = 1;
 		}
 	    }
+
 	};
 	warn $@ if $@;
 
diff --git a/PVE/Ceph/Services.pm b/PVE/Ceph/Services.pm
index 37621652..0448c7d1 100644
--- a/PVE/Ceph/Services.pm
+++ b/PVE/Ceph/Services.pm
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 
 use PVE::Ceph::Tools;
-use PVE::Cluster;
+use PVE::Cluster qw(cfs_read_file);
 use PVE::Tools qw(run_command);
 use PVE::RADOS;
 
@@ -71,6 +71,54 @@ sub ceph_service_cmd {
     run_command(['/bin/systemctl', $action, $service]);
 }
 
+sub get_services_info {
+    my ($type, $cfg, $rados) = @_;
+
+    my $result = {};
+    my $services = get_cluster_service($type);
+
+    foreach my $host (sort keys %$services) {
+	foreach  my $id (sort keys %{$services->{$host}}) {
+	    $result->{$id} = $services->{$host}->{$id};
+	    $result->{$id}->{host} = $host;
+	    $result->{$id}->{name} = $id;
+	    $result->{$id}->{state} = 'unknown';
+	    if ($result->{$id}->{service}) {
+		$result->{$id}->{state} = 'stopped';
+	    }
+	}
+    }
+
+    if (!$cfg) {
+	$cfg = cfs_read_file('ceph.conf');
+    }
+
+    foreach my $section (keys %$cfg) {
+	my $d = $cfg->{$section};
+	if ($section =~ m/^$type\.(\S+)$/) {
+	    my $id = $1;
+	    my $addr = $d->{"$type addr"} // $d->{"${type}_addr"} // $d->{host};
+	    $result->{$id}->{name} //= $id;
+	    $result->{$id}->{addr} //= $addr;
+	    $result->{$id}->{state} //= 'unknown';
+	    $result->{$id}->{host} //= $d->{host};
+	}
+    }
+
+    if (!$rados) {
+	$rados = PVE::RADOS->new();
+    }
+    my $metadata = $rados->mon_command({ prefix => "$type metadata" });
+    foreach my $service (@$metadata) {
+	$result->{$service->{name}}->{ceph_version_short} = $service->{ceph_version_short};
+	$result->{$service->{name}}->{ceph_version} = $service->{ceph_version};
+	$result->{$service->{name}}->{host} //= $service->{hostname};
+	$result->{$service->{name}}->{addr} //= $service->{addr};
+    }
+
+    return $result;
+}
+
 # MDS
 
 sub list_local_mds_ids {
-- 
2.11.0





More information about the pve-devel mailing list