Using Graphviz dot for ERDs, network diagrams and more

Using Graphviz dot for ERDs, network diagrams and more

I’ve mentioned Graphviz many a time on this blog.  It’s simple to use, yet very powerful.  The dot language is something that can be jotted down by hand in the simplest of all text editors, or generated programmatically.

The official website features a gallery, which demonstrates a wide range of graphs.  But I still wanted to blog a few examples from my recent use.

 

First of all, for those of you who are not familiar with the simplicity of dot, here is the shortest possible example.  Create a simple.dot file with the following content:

digraph G { 
    Hello->World;
}

Now you can generate a PNG image with the graph like so:

$ dot -Tpng < simple.dot > simple.png

And here is how it looks:

Simple graph

Beautiful, isn’t it?

Well, now that you have an idea, let me show you a couple of things that I’ve been doing with it recently.

First, the Entity Relationship Diagrams (ERDs).  There are, of course, specialized tools for drawing those, but I find it handy to sketch something quick, to discuss an idea with the team.  Both the dot file and an image can be part of the project source, managed with version control software, build scripts, etc.

Here is a simple example.  First, the code:

// Generate and preview PNG image with:
// dot -Tpng erd.dot > erd.png && eom erd.png
// 
// You'll need Graphviz installed
digraph G { 
    //  
    // Defaults
    //  

    // Box for entities
    node [shape=none, margin=0]

    // One-to-many relation (from one, to many)
    edge [arrowhead=crow, arrowtail=none, dir=both]

    //  
    // Entities
    //  
    Article [label=<
        <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
            <tr><td bgcolor="lightblue">Article</td></tr>
            <tr><td align="left">id: int(11)</td></tr>
            <tr><td align="left">author: int(11)</td></tr>
            <tr><td align="left">title: varchar(255)</td></tr>
            <tr><td align="left">content: longtext</td></tr>
            <tr><td align="left">created: datetime</td></tr>
            <tr><td align="left">modified: datetime</td></tr>
        </table>
    >]

    Comment [label=<
        <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
            <tr><td bgcolor="lightblue">Comment</td></tr>
            <tr><td align="left">id: int(11)</td></tr>
            <tr><td align="left">author: int(11)</td></tr>
            <tr><td align="left">content: longtext</td></tr>
            <tr><td align="left">created: datetime</td></tr>
            <tr><td align="left">modified: datetime</td></tr>
        </table>
    >]  

    //  
    // Relationships
    //  
    Article->Comment;
}

And now how it comes out in the PNG format:

ERD

Secondly, infrastructure documentation. Very similar to the above, but now with clusters, that allow grouping of things. Here’s the example.com network setup:

digraph G { 
    label = "Example.com Network"
    labelloc = top;

    node [shape=record];
    edge [dir=both];

    subgraph cluster_0 {
        label = "gw.example.com";

        eth0 [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>eth0</b></td></tr>
                <tr><td align="left">IP: 172.16.0.1</td></tr>
                <tr><td align="left">Mask: 255.255.255.0</td></tr>
            </table>
        >];
        eth1 [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>eth1</b></td></tr>
                <tr><td align="left">IP: 10.0.0.1</td></tr>
                <tr><td align="left">Mask: 255.255.255.0</td></tr>
            </table>
        >];
        eth2 [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>eth2</b></td></tr>
                <tr><td align="left">IP: 192.168.0.1</td></tr>
                <tr><td align="left">Mask: 255.255.255.0</td></tr>
            </table>
        >];
    }   

    subgraph cluster_1 {
        label = Internet;
        labelloc = bottom;
        InternetGW [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>ISP</b></td></tr>
                <tr><td align="left">IP: 10.0.0.254</td></tr>
            </table>
        >];

    }

    subgraph cluster_2 {
        label = Telephony;
        labelloc = bottom;
        TelephonyGW [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>Telecom</b></td></tr>
                <tr><td align="left">IP: 192.168.0.254</td></tr>
            </table>
        >];
    }

    subgraph cluster_3 {
        label = LAN;
        labelloc = bottom;

        sw1 [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>switch1.example.com</b></td></tr>
                <tr><td align="left">IP: 172.16.0.2</td></tr>
            </table>
        >];

        sw2 [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>switch2.example.com</b></td></tr>
                <tr><td align="left">IP: 172.16.0.3</td></tr>
            </table>
        >];

        ap1 [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>wifi1.example.com</b></td></tr>
                <tr><td align="left">IP: 172.16.0.4</td></tr>
            </table>
        >];

        ap2 [label=<
            <table border="0" cellborder="1" cellspacing="0" cellpadding="4">
                <tr><td bgcolor="lightblue"><b>wifi2.example.com</b></td></tr>
                <tr><td align="left">IP: 172.16.0.5</td></tr>
            </table>
        >];

        sw1 -> sw2;
        sw2 -> ap1;
        sw2 -> ap2;
    }


    eth0 -> sw1;
    eth1 -> InternetGW;
    eth2 -> TelephonyGW;

}

And here is the result:

network

And as I said, you can automatically generate the dot files either completely or partially. In fact, I’ve done it a while back, when I was trying to make sense of a large CakePHP project. Have a look at this post (rather outdated now).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值