2

Can this be done in PGSQL? I have a view which I created where hostname,ip, and datacenter are from one table, and ifdesc and if stats from another table. the view output looks like this:

hostname | ip     | datacenter | ifdesc           | ifadminstat | ifoperstat|
---------- ------------------------------------------------------------------
r1        1.1.1.1     dc       GigabitEthernet1/1      2             1
r1        1.1.1.1     dc       GigabitEthernet1/2      2             2
r1        1.1.1.1     dc       GigabitEthernet1/3      2             2
r1        1.1.1.1     dc       GigabitEthernet1/4      2             1
r1        1.1.1.1     dc       GigabitEthernet2/1      2             2
r1        1.1.1.1     dc       GigabitEthernet2/2      2             2
r2        2.2.2.2     dc       GigabitEthernet1/1      2             2
r2        2.2.2.2     dc       GigabitEthernet1/2      2             2

I need to get a count of "ifadminstat = 2" and "ifoperstat = 2" for all interfaces on each blade, for each router (for example... for r1, how many interfaces on blade 1 (GigabitEthernet1/1-48) have "ifadminstat = 2" and "ifoperstat = 2".

I am trying to do the counting in Postgresql then present the results on a website using PHP.

2 Answers 2

3
select hostname, ip, count(1)
  from table
 where ifadminstat = 2 and ifoperstat = 2
 group by hostname, ip
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. The problem I have is I need the count for every blade in the switch. For each address, and each blade, how many = 2. I believe I will need some sort of regex count(substring(ifdesc, 'Ethernet1/*)
0

You've already defined a view. Might as well make it more useful by plucking out the blades in the switch, etc.

CREATE VIEW ... AS
SELECT ... 
  substr(ifdesc, 1, strpos(ifdesc, '/') - 1) AS backplane_name,
  substr(ifdesc, strpos(ifdesc, '/') + 1) AS switch_port,
  ...

From there it becomes pretty trivial to write your query.

SELECT host, ip, backplane_name, count(1) AS active_ports
FROM table
WHERE ifadminstat = 2 AND ifoperstat = 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.