2929use App \Facades \LibrenmsConfig ;
3030use App \Facades \Rrd ;
3131use App \Models \Device ;
32- use Illuminate \Support \Facades \ DB ;
32+ use Illuminate \Support \Collection ;
3333use Illuminate \Support \Facades \Gate ;
3434use LibreNMS \Data \Graphing \AbstractGraph ;
3535use LibreNMS \Data \Graphing \Builders \MultiLineGraphBuilder ;
3636use LibreNMS \Data \Graphing \Builders \MultiSimplexSeparatedGraphBuilder ;
37- use LibreNMS \Exceptions \RrdGraphException ;
38- use LibreNMS \Util \Rewrite ;
3937
4038class ProcessorGraph extends AbstractGraph
4139{
4240 public string $ type = 'device ' ;
4341 public string $ subtype = 'processor ' ;
4442
4543 private Device $ device ;
44+ private Collection $ processors ;
4645
47-
48- public function __construct ( private readonly array $ vars = [])
49- {
46+ public function __construct (
47+ private readonly array $ vars = [],
48+ ) {
5049 $ this ->device = DeviceCache::get ($ this ->vars ['device ' ] ?? null );
50+ $ this ->processors = $ this ->device ->exists ? $ this ->device ->processors : new Collection ;
5151 }
5252
5353 public function authorize (): bool
5454 {
55- return Gate::allows ('view ' , $ this ->device );
55+ if ($ processor = $ this ->processors ->first ()) {
56+ return Gate::allows ('view ' , $ processor );
57+ }
58+
59+ return $ this ->device ->exists && Gate::allows ('view ' , $ this ->device );
5660 }
5761
5862 public function getGraphTitle (): string
@@ -62,42 +66,30 @@ public function getGraphTitle(): string
6266
6367 public function getRrdFiles (): array
6468 {
65- $ procs = DB ::table ('processors ' )->where ('device_id ' , $ this ->device ->device_id )->get ();
6669 $ files = [];
67- foreach ($ procs as $ proc ) {
68- $ rrd_filename = Rrd::name ($ this ->device ->hostname , ['processor ' , $ proc ->processor_type , $ proc ->processor_index ]);
69- if (Rrd::checkRrdExists ($ rrd_filename )) {
70- $ files [] = $ rrd_filename ;
71- }
70+ foreach ($ this ->processors as $ proc ) {
71+ $ files [] = Rrd::name ($ this ->device ->hostname , ['processor ' , $ proc ->processor_type , $ proc ->processor_index ]);
7272 }
7373 return $ files ;
7474 }
7575
7676 public function definition (array $ vars = []): array
7777 {
78- $ this ->authorize ();
79-
80- $ procs = DB ::table ('processors ' )->where ('device_id ' , $ this ->device ->device_id )->get ();
81- if ($ procs ->isEmpty ()) {
82- throw new RrdGraphException ('No Processors ' );
78+ if ($ this ->processors ->isEmpty ()) {
79+ throw new \LibreNMS \Exceptions \RrdGraphException ('No Processors ' );
8380 }
8481
85- $ rrd_list = [];
86- foreach ($ procs as $ proc ) {
82+ $ vars = array_merge ($ this ->vars , $ vars );
83+
84+ // Count how many files actually exist on disk for the layout
85+ $ rrd_count = 0 ;
86+ foreach ($ this ->processors as $ proc ) {
8787 $ rrd_filename = Rrd::name ($ this ->device ->hostname , ['processor ' , $ proc ->processor_type , $ proc ->processor_index ]);
8888 if (Rrd::checkRrdExists ($ rrd_filename )) {
89- $ descr = Rewrite::shortHrDevice ($ proc ->processor_descr );
90- $ rrd_list [] = [
91- 'filename ' => $ rrd_filename ,
92- 'descr ' => $ descr ,
93- 'ds ' => 'usage ' ,
94- ];
89+ $ rrd_count ++;
9590 }
9691 }
9792
98- $ vars = array_merge ($ this ->vars , $ vars );
99-
100- // Check if stacked processor graph is configured for this OS
10193 if (LibrenmsConfig::getOsSetting ($ this ->device ->os , 'processor_stacked ' )) {
10294 $ builder = (new MultiSimplexSeparatedGraphBuilder ())
10395 ->unitText ('Load % ' )
@@ -106,37 +98,29 @@ public function definition(array $vars = []): array
10698 ->colours ('oranges ' )
10799 ->scaleMin (0 )
108100 ->scaleMax (100 )
109- ->divider (count ( $ rrd_list ))
101+ ->divider (max ( 1 , $ rrd_count ))
110102 ->textOrig ()
111103 ->noTotal ();
112-
113- foreach ($ rrd_list as $ rrd ) {
114- $ builder ->addDataset (
115- filename: $ rrd ['filename ' ],
116- ds: $ rrd ['ds ' ],
117- description: $ rrd ['descr ' ]
118- );
119- }
120-
121- return $ builder ->build ($ vars );
104+ } else {
105+ $ builder = (new MultiLineGraphBuilder ())
106+ ->unitText ('Load % ' )
107+ ->units ('' )
108+ ->totalUnits ('% ' )
109+ ->colours ('mixed ' )
110+ ->scaleMin (0 )
111+ ->scaleMax (100 )
112+ ->noTotal ();
122113 }
123114
124- $ builder = (new MultiLineGraphBuilder ())
125- ->unitText ('Load % ' )
126- ->units ('' )
127- ->totalUnits ('% ' )
128- ->colours ('mixed ' )
129- ->scaleMin (0 )
130- ->scaleMax (100 )
131- ->noTotal ();
132-
133- foreach ($ rrd_list as $ rrd ) {
134- $ builder ->addDataset (
135- filename: $ rrd ['filename ' ],
136- ds: $ rrd ['ds ' ],
137- description: $ rrd ['descr ' ],
138- area: true
139- );
115+ foreach ($ this ->processors as $ proc ) {
116+ $ rrd_filename = Rrd::name ($ this ->device ->hostname , ['processor ' , $ proc ->processor_type , $ proc ->processor_index ]);
117+ if (Rrd::checkRrdExists ($ rrd_filename )) {
118+ if ($ builder instanceof MultiLineGraphBuilder) {
119+ $ builder ->addDataset ($ rrd_filename , 'usage ' , $ proc ->getFormattedDescription (), area: true );
120+ } else {
121+ $ builder ->addDataset ($ rrd_filename , 'usage ' , $ proc ->getFormattedDescription ());
122+ }
123+ }
140124 }
141125
142126 return $ builder ->build ($ vars );
0 commit comments