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

[Task] [OSPP] JAVA implements the native ipmi2 communication protocol #2726

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
835a9bf
feat: ipmi protocol using doc
gjjjj0101 Sep 19, 2024
9282917
feat: basic IPMI protocol implement support session manage and chassi…
gjjjj0101 Sep 19, 2024
db212d2
test: IPMI protocol unit test
gjjjj0101 Sep 19, 2024
8c838ff
docs: IPMI protocol monitor template
gjjjj0101 Sep 19, 2024
809a9af
Merge branch 'master' into ospp-impi-collector
gjjjj0101 Sep 19, 2024
e19c888
chore: add license header
gjjjj0101 Sep 20, 2024
10e2022
chore: fix markdown lint error
gjjjj0101 Sep 20, 2024
6f54d1d
Merge branch 'master' into ospp-impi-collector
Aias00 Sep 21, 2024
342f502
Merge branch 'master' into ospp-impi-collector
yuluo-yx Sep 22, 2024
3807769
chore: typo fix
gjjjj0101 Sep 24, 2024
711ed34
refactor: The deserialization process for the IPMI response no longer…
gjjjj0101 Sep 24, 2024
00184b0
Merge branch 'master' into ospp-impi-collector
gjjjj0101 Sep 24, 2024
c5384f1
Merge branch 'master' into ospp-impi-collector
Aias00 Sep 24, 2024
14f5171
Merge branch 'master' into ospp-impi-collector
Aias00 Sep 25, 2024
9d6a4b6
Merge branch 'master' into ospp-impi-collector
Aias00 Sep 25, 2024
b9c9584
refactor: Encapsulate the process of obtaining ipmi resources through…
gjjjj0101 Sep 25, 2024
388809a
feat: get sensor info
gjjjj0101 Sep 25, 2024
7aef945
docs: ipmi get sensor collector docs
gjjjj0101 Sep 25, 2024
2c22167
Merge branch 'master' into ospp-impi-collector
gjjjj0101 Sep 25, 2024
c553189
refactor: extract Generating Additional Keying Material function
gjjjj0101 Sep 26, 2024
b1790ed
Merge branch 'master' into ospp-impi-collector
Aias00 Sep 30, 2024
ef049fb
Merge branch 'master' into ospp-impi-collector
Aias00 Oct 8, 2024
fda3b6e
Merge branch 'master' into ospp-impi-collector
yuluo-yx Oct 10, 2024
edc6ec4
Merge branch 'master' into ospp-impi-collector
Aias00 Oct 13, 2024
fcc82eb
Merge branch 'master' into ospp-impi-collector
Aias00 Oct 16, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.collector.collect.ipmi2;

import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.collector.collect.AbstractCollect;
import org.apache.hertzbeat.collector.collect.common.cache.CacheIdentifier;
import org.apache.hertzbeat.collector.collect.common.cache.ConnectionCommonCache;
import org.apache.hertzbeat.collector.collect.ipmi2.cache.IpmiConnect;
import org.apache.hertzbeat.collector.collect.ipmi2.client.IpmiClient;
import org.apache.hertzbeat.collector.collect.ipmi2.client.IpmiConnection;
import org.apache.hertzbeat.collector.collect.ipmi2.client.IpmiHandlerManager;
import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.IpmiProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.Optional;

/**
* Ipmi collect implementation
*/
@Slf4j
public class IpmiCollectImpl extends AbstractCollect {

private final ConnectionCommonCache<CacheIdentifier, IpmiConnect> connectionCommonCache;

private final IpmiHandlerManager ipmiHandlerManager;

public IpmiCollectImpl() {
connectionCommonCache = new ConnectionCommonCache<>();
ipmiHandlerManager = new IpmiHandlerManager();
}


@Override
public void preCheck(Metrics metrics) throws IllegalArgumentException {
if (metrics == null || metrics.getIpmi() == null) {
throw new IllegalArgumentException("Ipmi collect must has ipmi params");
}
IpmiProtocol ipmiProtocol = metrics.getIpmi();
Assert.hasText(ipmiProtocol.getHost(), "Ipmi Protocol host is required.");
Assert.hasText(ipmiProtocol.getPort(), "Ipmi Protocol port is required.");
Assert.hasText(ipmiProtocol.getUsername(), "Ipmi Protocol username is required.");
Assert.hasText(ipmiProtocol.getPassword(), "Ipmi Protocol password is required.");
}

@Override
public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) {
IpmiConnection connection = null;
try {
connection = getIpmiConnection(metrics.getIpmi());
} catch (Exception e) {
log.error("Ipmi session create error: {}", e.getMessage());
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg(e.getMessage());
return;
}
try {
connection.getResource(builder, metrics);
} catch (IOException e) {
log.error("Get Ipmi {} detail resource error: {}", metrics.getName(), e.getMessage());
}
}

@Override
public String supportProtocol() {
return DispatchConstants.PROTOCOL_IPMI;
}


private IpmiConnection getIpmiConnection(IpmiProtocol ipmiProtocol) throws Exception {
CacheIdentifier identifier = CacheIdentifier.builder()
.ip(ipmiProtocol.getHost())
.port(ipmiProtocol.getPort())
.username(ipmiProtocol.getUsername())
.password(ipmiProtocol.getPassword())
.build();
IpmiConnection connection = null;
Optional<IpmiConnect> cacheOption = connectionCommonCache.getCache(identifier, true);
if (cacheOption.isPresent()) {
IpmiConnect ipmiConnect = cacheOption.get();
connection = ipmiConnect.getConnection();
if (connection == null || !connection.isActive()) {
connection = null;
connectionCommonCache.removeCache(identifier);
}
}
if (connection != null) {
return connection;
}
IpmiClient ipmiClient = IpmiClient.create(ipmiProtocol);
connection = ipmiClient.connect();
connectionCommonCache.addCache(identifier, new IpmiConnect(connection));
return connection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.collector.collect.ipmi2.cache;


import org.apache.hertzbeat.collector.collect.common.cache.AbstractConnection;
import org.apache.hertzbeat.collector.collect.ipmi2.client.IpmiConnection;

/**
* ipmi connect session
*/
public class IpmiConnect extends AbstractConnection<IpmiConnection> {

private final IpmiConnection ipmiConnection;

public IpmiConnect(IpmiConnection ipmiConnection) {
this.ipmiConnection = ipmiConnection;
}

@Override
public IpmiConnection getConnection() {
return ipmiConnection;
}

@Override
public void closeConnection() throws Exception {
if (ipmiConnection != null) {
ipmiConnection.close();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.collector.collect.ipmi2.client;

import java.io.IOException;
import java.security.SecureRandom;

import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.payload.RakpMessage1;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.payload.RakpMessage2;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.payload.RakpMessage3;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.payload.RakpMessage4;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.payload.RmcpPlusOpenSessionRequest;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.payload.RmcpPlusOpenSessionResponse;
import org.apache.hertzbeat.common.entity.job.protocol.IpmiProtocol;

/**
* IpmiClient used to connect to a remote Ipmi server
*/
public class IpmiClient {

UdpConnection connection;
private final String host;
private final Integer port;
private final String username;
private final String password;

public IpmiClient(String host, Integer port, String username, String password) throws IOException {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
connection = new UdpConnection(host, port);
}

public static IpmiClient create(IpmiProtocol ipmiProtocol) throws IOException {
return new IpmiClient(ipmiProtocol.getHost(), Integer.parseInt(ipmiProtocol.getPort()),
ipmiProtocol.getUsername(), ipmiProtocol.getPassword());
}

public IpmiConnection connect() throws IOException {
IpmiSession session = newSession(username, password);

connection.send(session, new RmcpPlusOpenSessionRequest());
RmcpPlusOpenSessionResponse rmcpPlusOpenSessionResponse = connection.receive(session, RmcpPlusOpenSessionResponse.class);
session.setSystemSessionId(rmcpPlusOpenSessionResponse.systemSessionId);

session.generateConsoleRandomNumber();
connection.send(session, new RakpMessage1());
RakpMessage2 rakpMessage2 = connection.receive(session, RakpMessage2.class);
session.setSystemRandomNumber(rakpMessage2.systemRandom);
session.setSystemGuid(rakpMessage2.systemGuid);


session.generateSik();
session.generateK1();
session.generateK2();
connection.send(session, new RakpMessage3());
connection.receive(session, RakpMessage4.class);

session.setConnected(true);
return new IpmiConnection(session, connection);
}

public IpmiSession newSession(String username, String password) {
SecureRandom random = new SecureRandom();
IpmiSession session = new IpmiSession(random.nextInt());
session.setUserName(username);
session.setPassword(password);
return session;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.collector.collect.ipmi2.client;

import java.io.IOException;
import org.apache.hertzbeat.collector.collect.ipmi2.client.handler.IpmiHandler;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.command.messaging.CloseSessionRequest;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.ipmi.command.messaging.CloseSessionResponse;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.message.CollectRep;

/**
* IpmiConnection used for sending ipmi request
*/
public class IpmiConnection implements AutoCloseable {

IpmiSession session;

UdpConnection udpConnection;

IpmiHandlerManager handlerManager = new IpmiHandlerManager();

private volatile boolean active = true;

IpmiConnection(IpmiSession session, UdpConnection udpConnection) {
this.session = session;
this.udpConnection = udpConnection;
}

public void getResource(CollectRep.MetricsData.Builder builder, Metrics metrics) throws IOException {
IpmiHandler handler = handlerManager.getHandler(metrics.getName());
if (handler == null) {
throw new RuntimeException("no handler for " + metrics.getIpmi().getType());
}
handler.handler(session, udpConnection, builder, metrics);
}


@Override
public void close() throws IOException {
udpConnection.send(session, new CloseSessionRequest(session.getSystemSessionId()));
udpConnection.receive(session, CloseSessionResponse.class);
udpConnection.close();
session = null;
active = false;
}

public boolean isActive() {
return this.active;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.collector.collect.ipmi2.client;

import java.nio.ByteBuffer;
import org.apache.hertzbeat.collector.collect.ipmi2.protocol.rmcp.RmcpPacket;

/**
* IPMI encoder and decoder
*/
public class IpmiEncoderDecoder {

public static ByteBuffer encode(IpmiPacketContext context, RmcpPacket rmcpPacket) {
int length = rmcpPacket.getWireLength(context.getIpmiSession());
ByteBuffer buffer = ByteBuffer.allocate(length);
rmcpPacket.toWire(context, buffer);
buffer.flip();
return buffer;
}

public static RmcpPacket decode(IpmiPacketContext context, ByteBuffer buffer) {
RmcpPacket rmcpPacket = new RmcpPacket();
rmcpPacket.fromWire(context, buffer);
return rmcpPacket;
}
}
Loading
Loading