Openstackのtopo図
16166 ワード
詳細
http://haoningabc.iteye.com/blog/2317951Openstackのインストールが完了しました
http://haoningabc.iteye.com/blog/2322169neutronの試験を終える
複雑なネットワーク関係はまだ取得できません.
TOpo図を作る
スクリプトを書いてnovaとneutronのテーブルの構造を表示します
mysql_openstack.sh:
./mysql_openstack.sh neutron
./mysql_openstack.sh nova
novaライブラリのinstanceを観察
neutronライブラリ、networks、routers、subnets、ports
これらの表はneutronの基礎概念を理解している.
ネットワーク、サブネット、ルータ、ポート
実はlinuxの
veth pair,tap,ブリッジ,vxlan
等概念の組み合わせ
Openstackコマンドを使用する場合
Javaのdemoを簡単に書きます.
固定フォーマットのjsonを生成し、
nofloに転送topo図として表示
http://haoningabc.iteye.com/blog/2317951Openstackのインストールが完了しました
http://haoningabc.iteye.com/blog/2322169neutronの試験を終える
複雑なネットワーク関係はまだ取得できません.
TOpo図を作る
スクリプトを書いてnovaとneutronのテーブルの構造を表示します
mysql_openstack.sh:
#!/bin/sh
#for i in `awk ' {if(NR>4 && NR<40)print $2};' a.log `
mysql_user=root
mysql_password=haoning
mysql_host=mcon
if [ "$1" = "" ]
then
echo "please use ./mysql_openstack.sh [dbname], for example: ./mysql_openstack.sh keystone";
echo "this will exit."
exit 0;
fi
echo "use db " $1
for i in ` mysql -u$mysql_user -h$mysql_host -p$mysql_password $1 -e "show tables" |awk ' {if(NR>1)print $1};'`
do
if [ $i != "ml2_vxlan_allocations" ]
then
echo "mysql -u$mysql_user -h$mysql_host -p$mysql_password $1 -e \"select * from \`$i\`\"";
mysql -u$mysql_user -h$mysql_host -p$mysql_password $1 -e "select * from \`$i\`";
fi
done
./mysql_openstack.sh neutron
./mysql_openstack.sh nova
novaライブラリのinstanceを観察
neutronライブラリ、networks、routers、subnets、ports
これらの表はneutronの基礎概念を理解している.
ネットワーク、サブネット、ルータ、ポート
実はlinuxの
brctl show
ip netns
bridge fdb
ip neigh
veth pair,tap,ブリッジ,vxlan
等概念の組み合わせ
Openstackコマンドを使用する場合
nova list
neutron net-list
neutron subnet-list
neutron router-list
neutron port-list
Javaのdemoを簡単に書きます.
固定フォーマットのjsonを生成し、
nofloに転送topo図として表示
package openstacktopo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
//import net.sf.json.JSONObject;
//https://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/
public class Topo {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection(String db) {
String url = "jdbc:mysql://192.168.139.251:3306/"+db;
String username = "root";
String password = "haoning";
Connection con = null;
try {
con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public Map getInstances() {//
Map vm_processes=new HashMap();
ResultSet rs = null;
Connection con = null;
try {
con = getConnection("nova");
PreparedStatement ps = null;
String sql = "select i.uuid,i.display_name,i.launched_on,i.vm_state from instances i where deleted =0 ";
ps = (PreparedStatement) con.prepareStatement(sql);
if (ps.execute()) {
rs = ps.getResultSet();
StringBuffer b = new StringBuffer();
while (rs.next()) {
JSONObject instance=new JSONObject();
JSONObject metadata=new JSONObject();
//System.out.println(rs.getString("uuid")+"\t");
//System.out.println(rs.getString("display_name")+"\t");
instance.put("component", "vm/"+rs.getString("uuid"));
metadata.put("type", "vm");
metadata.put("vm_id", rs.getString("uuid"));
metadata.put("vm_status", rs.getString("vm_state"));
metadata.put("label", rs.getString("display_name"));
instance.put("metadata", metadata);
//System.out.println(instance);
vm_processes.put("vm/"+rs.getString("uuid"), instance);
}
} else {
int i = ps.getUpdateCount();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
} catch (Exception e2) {}
}
return vm_processes;
}
public List get_instance_net() {// vm
List list_i_net=new ArrayList();
ResultSet rs = null;
Connection con = null;
try {
con = getConnection("nova");
PreparedStatement ps = null;
String sql = "select iic.network_info,i.uuid,i.display_name,i.launched_on from instance_info_caches iic , instances i where i.id=iic.id and i.deleted = 0";
ps = (PreparedStatement) con.prepareStatement(sql);
if (ps.execute()) {
rs = ps.getResultSet();
StringBuffer b = new StringBuffer();
while (rs.next()) {
//System.out.println(rs.getString("uuid")+"\t");//vm-id
//System.out.println(rs.getString("display_name")+"\t");//vm-name
String network_info = rs.getString("network_info");
//System.out.println(rs.getString("network_info"));
JSONArray ja = new JSONArray().fromObject(network_info);
for(int i=0;i get_router_net() {// router
List list_router_net=new ArrayList();
ResultSet rs = null;
Connection con = null;
try {
con = getConnection("neutron");
PreparedStatement ps = null;
String sql = "select * from routerports rp ,ports p,ipallocations i where rp.port_id=p.id and i.port_id=p.id";
ps = (PreparedStatement) con.prepareStatement(sql);
if (ps.execute()) {
rs = ps.getResultSet();
StringBuffer b = new StringBuffer();
while (rs.next()) {
JSONObject one_connection=new JSONObject();
String ip_address = rs.getString("ip_address");
if("network:router_interface".equals(rs.getString("port_type"))){
one_connection.put("src", new JSONObject().fromObject("{\"process\":\"router/"+rs.getString("router_id")+"\",\"port\":\""+ip_address+"\"}"));
one_connection.put("tgt", new JSONObject().fromObject("{\"process\":\"switch/"+rs.getString("network_id")+"\",\"port\":\""+"in"+"\"}"));
}else{//network:router_gateway
//one_connection.put("src", new JSONObject().fromObject("{\"process\":\"router/"+rs.getString("router_id")+"\",\"port\":\""+jo.get("port_id")+"\"}"));
one_connection.put("src", new JSONObject().fromObject("{\"process\":\"switch/"+rs.getString("network_id")+"\",\"port\":\""+"out"+"\"}"));
one_connection.put("tgt", new JSONObject().fromObject("{\"process\":\"router/"+rs.getString("router_id")+"\",\"port\":\""+ip_address+"\"}"));
}
//System.out.println(one_connection);
list_router_net.add(one_connection);
//System.out.println();
}
} else {
int i = ps.getUpdateCount();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
} catch (Exception e2) {}
}
return list_router_net;
}
public Map getRouters() {
Map routers=new HashMap();
Connection con = null;
ResultSet rs = null;
try {
con = getConnection("neutron");
PreparedStatement ps = null;
String sql = "select * from routers";
ps = (PreparedStatement) con.prepareStatement(sql);
if (ps.execute()) {
rs = ps.getResultSet();
StringBuffer b = new StringBuffer();
while (rs.next()) {
JSONObject instance=new JSONObject();
JSONObject metadata=new JSONObject();
instance.put("component", "router/"+rs.getString("id"));
metadata.put("type", "router");
metadata.put("router_id", rs.getString("id"));
metadata.put("router_status", rs.getString("status"));
metadata.put("label", rs.getString("name"));
instance.put("metadata", metadata);
//System.out.println(instance);
routers.put("router/"+rs.getString("id"), instance);
}
} else {
int i = ps.getUpdateCount();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
} catch (Exception e2) {}
}
return routers;
}
public Map getNetworkExternal() {//
Map external_map = new HashMap();
Connection con = null;
ResultSet rs = null;
try {
con = getConnection("neutron");
PreparedStatement ps = null;
String sql = "select * from networkrbacs where action='access_as_external'";//where tenant_id= scsssdfs;
ps = (PreparedStatement) con.prepareStatement(sql);
if (ps.execute()) {
rs = ps.getResultSet();
StringBuffer b = new StringBuffer();
while (rs.next()) {
external_map.put(rs.getString("object_id"),"access_as_external");
}
} else {
int i = ps.getUpdateCount();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
} catch (Exception e2) {}
}
//System.out.println(external_map.size());
return external_map;
}
public Map getNetworkShared() {//
Map shared_map = new HashMap();
Connection con = null;
ResultSet rs = null;
try {
con = getConnection("neutron");
PreparedStatement ps = null;
String sql = "select * from networkrbacs where action='access_as_shared'";//where tenant_id= scsssdfs;
ps = (PreparedStatement) con.prepareStatement(sql);
if (ps.execute()) {
rs = ps.getResultSet();
StringBuffer b = new StringBuffer();
while (rs.next()) {
shared_map.put(rs.getString("object_id"), "access_as_shared");
}
} else {
int i = ps.getUpdateCount();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
} catch (Exception e2) {}
}
//System.out.println(shared_map.size());
return shared_map;
}
public Map getSwitchs() {
Map switchs=new HashMap();
Connection con = null;
ResultSet rs = null;
Map shared_map = getNetworkShared();
Map external_map = getNetworkExternal();
try {
con = getConnection("neutron");
PreparedStatement ps = null;
String sql = "select * from networks";//where tenant_id= scsssdfs;
ps = (PreparedStatement) con.prepareStatement(sql);
if (ps.execute()) {
rs = ps.getResultSet();
StringBuffer b = new StringBuffer();
while (rs.next()) {
JSONObject instance=new JSONObject();
JSONObject metadata=new JSONObject();
instance.put("component", "switch/"+rs.getString("id"));
metadata.put("type", "switch");
metadata.put("switch_id", rs.getString("id"));
metadata.put("switch_status", rs.getString("status"));
metadata.put("label", rs.getString("name"));
if(shared_map.get(rs.getString("id"))!=null){
metadata.put("shared", "true");
}
if(external_map.get(rs.getString("id"))!=null){
metadata.put("router_external", "true");
}
instance.put("metadata", metadata);
//System.out.println(instance);
switchs.put("switch/"+rs.getString("id"), instance);
}
} else {
int i = ps.getUpdateCount();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
con.close();
} catch (Exception e2) {}
}
return switchs;
}
public JSONObject getTopo(){
JSONObject topojson = new JSONObject();
JSONObject processes = new JSONObject();
Map ins =getInstances();
Iterator> it = ins.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = it.next();
processes.put(entry.getKey(), entry.getValue());
}
Map routers= getRouters();
Iterator> router = routers.entrySet().iterator();
while (router.hasNext()) {
Map.Entry entry = router.next();
processes.put(entry.getKey(), entry.getValue());
}
Map switchs= getSwitchs();
Iterator> switcher = switchs.entrySet().iterator();
while (switcher.hasNext()) {
Map.Entry entry = switcher.next();
processes.put(entry.getKey(), entry.getValue());
}
topojson.put("processes", processes);
List lin = get_instance_net();
List lrn = get_router_net();
for(int i=0;i