1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.rrd;
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import javax.management.MalformedObjectNameException;
26
27 import org.apache.commons.configuration.Configuration;
28 import org.neo4j.kernel.AbstractGraphDatabase;
29 import org.neo4j.server.configuration.Configurator;
30 import org.rrd4j.ConsolFun;
31 import org.rrd4j.DsType;
32 import org.rrd4j.core.RrdDb;
33 import org.rrd4j.core.RrdDef;
34
35 public class RrdFactory
36 {
37 public static final int STEP_SIZE = 3000;
38 public static final int STEPS_PER_ARCHIVE = 750;
39 private final Configuration config;
40
41 public RrdFactory( Configuration config )
42 {
43
44 this.config = config;
45 }
46
47 public RrdDb createRrdDbAndSampler( AbstractGraphDatabase db,
48 JobScheduler scheduler ) throws MalformedObjectNameException, IOException
49 {
50 Sampleable[] sampleables = new Sampleable[]{
51 new MemoryUsedSampleable(),
52 new NodeIdsInUseSampleable( db ),
53 new PropertyCountSampleable( db ),
54 new RelationshipCountSampleable( db )
55 };
56
57 String basePath = config.getString( Configurator.RRDB_LOCATION_PROPERTY_KEY, getDefaultDirectory( db ) );
58 RrdDb rrdb = createRrdb( basePath, STEP_SIZE, STEPS_PER_ARCHIVE, sampleables );
59
60 RrdSampler sampler = new RrdSampler( rrdb.createSample(), sampleables );
61 RrdJob job = new RrdJob( sampler );
62 scheduler.scheduleToRunEveryXSeconds( job, 3 );
63 return rrdb;
64 }
65
66 private String getDefaultDirectory( AbstractGraphDatabase db )
67 {
68 return new File( db.getStoreDir(), "rrd" ).getAbsolutePath();
69 }
70
71 protected RrdDb createRrdb( String inDirectory, int stepSize, int stepsPerArchive,
72 Sampleable... sampleables ) throws IOException
73 {
74 if ( !new File( inDirectory ).exists() )
75 {
76 RrdDef rrdDef = createRrdDb( inDirectory, stepSize );
77 defineDataSources( stepSize, rrdDef, sampleables );
78 addArchives( stepsPerArchive, rrdDef );
79 return new RrdDb( rrdDef );
80 } else
81 {
82 return new RrdDb( inDirectory );
83 }
84 }
85
86 private static void addArchives( int stepsPerArchive, RrdDef rrdDef )
87 {
88
89 rrdDef.addArchive( ConsolFun.AVERAGE, 0.5, 1, stepsPerArchive );
90
91
92 rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 10, stepsPerArchive );
93
94
95 rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 50, stepsPerArchive );
96
97
98 rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 300, stepsPerArchive );
99
100
101 rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 1300, stepsPerArchive );
102
103
104 rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 15000, stepsPerArchive * 5 );
105 }
106
107 private static void defineDataSources( int stepSize, RrdDef rrdDef, Sampleable[] sampleables )
108 {
109 for ( Sampleable sampleable : sampleables )
110 {
111 rrdDef.addDatasource( sampleable.getName(), DsType.GAUGE, stepSize, 0, Long.MAX_VALUE );
112
113 }
114 }
115
116 private static RrdDef createRrdDb( String inDirectory, int stepSize )
117 {
118 RrdDef rrdDef = new RrdDef( inDirectory, stepSize );
119
120 return rrdDef;
121 }
122 }